Home Forums Weaver Xtreme Theme How do I use a function result within a shortcode (specifically parallax)

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #18325
    Qoheleth
    Participant
    I’ve inserted some functions into the Actions & Filters area. rms_get_S2BG_url returns the url of a background image uploaded to the site based on a theme setting in the user’s profile. The function works as it should, but I can’t seem to figure out how to embed the results into the Parallax short code. What I get instead is just the code itself, echoed to the page. What am I doing wrong?
    Here’s the code in the page that”s supposed to show the parallax as well as a parallax command where I simply inserted the url manually so I can see how it would look if it was working properly.
    [php] $plax_url=rms_get_S2BG_url(‘worship’); echo $plax_url.”</br>”; echo “[parallax img="" . $plax_url . "" height=200]Worship Services Index[/parallax]“[/php]

    [parallax img='http://www.disciplesnet.org/wp-content/uploads/worship-classic.jpg' height=200]Worship Services Index[/parallax]
    #24822
    scrambler
    Moderator
    I dont know for sure, but if I had to guess this means that the page content is only interpreted once.
    The way you have it, it would have to be interpreted twice, once to interpret the php that produces the shortcode, then a second time to interpret the resulting shortcode.
    Unless someone who knows WP and PHP logic better than me comments, I would try with the PHP inside the shortcode. Something like below (do check the quote syntax as I am not savy in PHP)
    [parallax img=[php] $plax_url=rms_get_S2BG_url(‘worship’); echo ‘”‘ . $plax_url. ‘”‘ ;[/php] height=200]Worship Services Index[/parallax]
    #24823
    Qoheleth
    Participant

    Yes, I was pretty sure it was something about not interpreting in the right order or only once. At any rate, the code you suggested does indeed give a parallax segment, but the background image comes our url(http://www.disciplesnet.org/test-page/%5Bphp) (the url of the page itself eith “[php” appended) and the text overlay is $plax_url=rms_get_S2BG_url(‘worship’); echo ‘”‘ . $plax_url. ‘”‘ ;[/php] height=200]Worship Services Index.

    Incidentally, the following code does work to insert the full bacground image into the page:
    [php] $plax_url=rms_get_S2BG_url(‘worship’); echo ‘<img src=”‘ . $plax_url . ‘”>’;[/php]
    but putting the <img src= and > bits outside of the php code doesnt.
    #24824
    scrambler
    Moderator
    @Qoheleth Doing some search I came across

    https://wordpress.org/support/topic/how-do-i-execute-shortcode-in-my-php-code-only

    So I tried
    [php] $plax_url=rms_get_S2BG_url(‘worship’); echo do_shortcode(“[parallax img=" . $plax_url . " height=200]Worship Services Index[/parallax]“); [/php]
    And that appears to work
    The echo do_shortcode is the key here and you would have been better searching the codex or developer sites for this kind of question 🙂
    #24825
    Weaver
    Keymaster

    Sorry I didn’t see this sooner, but indeed, if you want to see output of a shortcode from within PHP code, you have to wrap it with do_shortcode().

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.