0

I am trying to create a shortcode for 2 of my theme option. Basically I want to grabe my theme options to my shortcode so that I can display them anywhere on my posts something like this:

[ads_1] [ads_2]

However when I tried to get the options on my shortcode it wont work at all and its giving me an error

Parse error: syntax error, unexpected 'theme_settings' (T_STRING) in C:\xampp\htdocs\themewp\wp-content\themes\bots_final\shortcodes.php on line 86

Here's my snippet for my shortcode where in I am trying to grab my theme option data:

add_shortcode('ads_1', function($atts){
    return '
    <div>
        <?php $options = get_option( 'theme_settings' ); ?>
        <?php echo $options['banner1']; ?>
    </div>';

});

I am trying to grab the options from my Theme options page. Check my theme options code:

<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>

Any idea what's the problem why I can't grab it?

  • Please don't cross post across diffirent platforms. Choose one platform and stick with it. Thank you. – Pieter Goosen Aug 12 '14 at 5:06
  • Ok sorry Pieter – user2918348 Aug 12 '14 at 7:27
  • No problem :-). And thank you for keeping this mind. Enjoy – Pieter Goosen Aug 12 '14 at 7:32
0

First of all, never use anonymous functions, never. Anonymous functions can't be removed later by plugins or themes

Secondly, have a look at the Shortcode API on how to properly create a shortcode.

Thirdly, this is not a wordpress specific problem, but pure php. You are using html and php together without the proper syntax to differentiate between html and php. Here is your shortcode function as it is suppose to be one

add_shortcode('ads_1', 'my_shortcode' );

function my_shortcode($atts){
    $options = get_option( 'theme_settings' );

        $output = '<div>' . $options['banner1'] . '</div>';

    return $output;

}
  • What do you Anonymous functions can't be removed later by plugins or themes? Can you please explain? – user2918348 Aug 12 '14 at 7:18
  • What I mean by that is, say you distribute your theme, and another person don't like the specific function and needs to remove it or alter it, or even call it back via a child theme or plugin, how are they going to do it? Change made my an anonymous function is permanent and can't be revesed – Pieter Goosen Aug 12 '14 at 7:23
0

The error you are receiving and most likely the reason it isnt working is because of your shortcode function. It looks like you are trying to redeclare php inside of the return. Try this (notice I dont have the

function ads_one_shortcode($content = null) {

    $options = get_option( 'theme_settings' );

    return '<div>
            ' . $options['banner1'] . '
        </div>';
}
add_shortcode("ads_1", "ads_one_shortcode");

See Add Shortcode on the Wordpress codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.