WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Including php code within a shortcode - help! (3 posts)

  1. aliceralph
    Member
    Posted 1 year ago #

    Hi guys

    I am struggling with what is probably quite a simple bit of PHP and wondering if anybody could help?

    I have a shortcode embedded in my template - working perfectly...

    <?php echo do_shortcode('[mwi_product sku="" title="true" title_tag="h2" desc="true" img="true" img_width="200" price="true" type="add" btn_color="blue" btn_link="button" cols="3"/]'); ?>

    (This is the shortcode for Magento WordPress integration - http://magentowp.com)

    <<< Notice that the 'mwi_product sku' box is empty. I want the client to be able to update this info easily themselves using custom metaboxes in the theme settings. For this I have a piece of php which calls the SKU information from a text metabox:

    <?php
    	    $options = get_option('sample_theme_options');
    	    echo $options['teaskus'];
    	?>

    Soooo basically I want to merge the two items above. I am hoping I could do something like the following but it breaks the template:

    <?php echo do_shortcode('[mwi_product sku="' . $options = get_option('sample_theme_options');
    	echo $options['teaskus']; . '" title="true" title_tag="h2" desc="true" img="true" img_width="200" price="true" type="add" btn_color="blue" btn_link="button" cols="3"/]'); ?>

    Any ideas?? Thanking all you wonderful WordPressers in advance.

  2. catacaustic
    Member
    Posted 1 year ago #

    There's a lot of errors in your PHP code.

    1. You can't use $options = get_options() inside a string output. That needs to be on it's own line.
    2. As you're using $options in other places I'm assuming that it's an array, and you can't output an array directly to a string output as you've tried in the first echo line
    3. You've tried to append a string after you've closed off a line with a semi-colan ;.

    Now I don't know the name of the value that you're trying to get, but here's my best guess at what you're trying to do...

    <?php
    	$options = get_option('sample_theme_options');
    	echo do_shortcode('[mwi_product sku="'.$options['teaskus'].'" title="true" title_tag="h2" desc="true" img="true" img_width="200" price="true" type="add" btn_color="blue" btn_link="button" cols="3"/]');
    ?>
  3. aliceralph
    Member
    Posted 1 year ago #

    Aha! Worked perfectly! Thank you so much, this has got me out of a fix.

    I am still trying to learn to write custom php and haven't gotten far beyond the basics so this is really helpful - and thanks also for taking the time to include an explanation of what's happening.

    Cheers! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic