WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] Placing a php call inside a shortcode in the page template

[Resolved] Placing a php call inside a shortcode in the page template

  • PTM Groups
    Member

    @ptm-groups

    Hi, I’ve looked here already: http://codex.wordpress.org/Function_Reference/do_shortcode

    But I don’t believe the answer is there.

    What I’m trying to do is this:

    I’ve created a custom field called ‘site_inspection’. The value of this field is a url.

    I want to insert the value of this field (the url) into a specific button shortcode in the page template so that the admin can enter the url into the field and the code will automatically insert that url into the button shortcode and display the completed button on the page.

    I can get the shortcode php to work if I hardcode the url, and the site_inspection field also works fine separately, but when I try to replace the hardcoded url with the php for the site_inspection field it doesn’t work.

    See Below

    <?php echo do_shortcode('[button link="<?php if(get_field('site_inspection')){echo get_field('site_inspection') ;}?>" type="icon" icon="search" newwindow="yes"] Site InSpection[/button]'); ?>

    I have also tried:
    <?php echo do_shortcode('[button link=" . (get_field('site_inspection')) ? (get_field('site_inspection') : ('') . ' type="icon" icon="search" newwindow="yes"] Site InSpection[/button]'); ?>

    With no success.

    I feel like I’m missing something simple in my syntax, but I can’t spot it. Any help would be appreciated.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • codingpet
    Member

    @codingpet

    Hi,

    For the first one, you added <?php ?> twice. Also, is get_field a custom function you defined? The custom fields, I can use the code below to get the URL. You may take a look at the difference from yours. I used get_post_meta to get the value and then add it to shortcode. You can add the necessary conditional operators.

    <?php $site_inspection = get_post_meta($post->ID, ‘site_inspection’, true); ?>
    <?php echo do_shortcode(‘[button link=”‘.$site_inspection.'” type=”icon” icon=”search” newwindow=”yes”] Site InSpection[/button]’); ?>

    codingpet.com

    PTM Groups
    Member

    @ptm-groups

    That worked perfectly! You’re a life saver, thank you 🙂

    codingpet
    Member

    @codingpet

    You are welcome. 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] Placing a php call inside a shortcode in the page template’ is closed to new replies.