• Resolved khunmax

    (@khunmax)


    Hi

    Thanks for a clever plugin.

    I am trying to set up rotating banner ads in Woo on both shop/archive pages and on individual product pages.

    I have tried literally dozens of plugin from the WP repository and none of them are any good. Either not enough functions or full of bloat.

    I figured I could just use your plugin to create some shortcodes and then insert them into woo using hooks.

    And

    Step one I created to functions.php snippets to insert the shortcodes into woo pages:

    // ADD SHORTCODE TO TOP OF SINGLE PRODUCT 
    
    add_action( 'woocommerce_before_single_product', 'enfold_customization_extra_product_content', 15 );
    function enfold_customization_extra_product_content() {
        echo do_shortcode("[my-shortcode]");
    }
    
    // ADD SHORTCODE TO TOP OF SHOP AND ARCHIVE PAGES
    
    add_action( 'woocommerce_archive_description',
         'woocommerce_archive_description', 15 );
    function woocommerce_archive_description() {
        echo do_shortcode("[my-shortcode]");
    }

    This works fine and the “dead” shortcodes appear where expected.

    I then also added the following php to my functions.php file:

    $banner1 = '<a href="https://targetwebsite.com" target="_blank"><img src="https://test.com/wp-content/uploads/2017/11/greenbanner.jpg" alt="BANNER1_ALT" title="BANNER1_TITLE"></a>';
    $banner2 = '<a href="https://targetwebsite.com" target="_blank"><img src="https://test.com/wp-content/uploads/2017/11/redbanner.jpg" alt="BANNER2_ALT" title="BANNER2_TITLE"></a>';
    $banner3 = '<a href="https://targetwebsite.com" target="_blank"><img src="https://test.com/wp-content/uploads/2017/11/blackbanner.jpg" alt="BANNER3_ALT" title="BANNER3_TITLE"></a>';
    $banners = array($banner1, $banner2, $banner3);
    shuffle($banners);

    I then added the following to a shortcoder edit page:

    <div>
      <?php print $banners[0] ?>
    </div>

    And named the resulting shortcode Banners. The result was the following shortcode:

    [sc name="Banners"]

    But…when I went to replace [my-shortcode] with [sc name=”Banners”] in my functions.php file I get the following error message:

    Syntax error, unexpected T_string, expecting ',' or ')'

    As a result the addition of your shortcode to my functions.php file breaks my site.

    Is there a way to change the name of the shortcode created by your plugin so that it does NOT include “” ?

    Or if not do you see any way to make the arrangement I have explained above work?

    Thanks in advance for your kind assistance.

    Max

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter khunmax

    (@khunmax)

    I found this article which is relevant to the issue:

    https://wordpress.stackexchange.com/questions/275077/shortcode-parse-error-wrong-syntax

    You kind assistance here is much appreciated.

    Plugin Author vaakash

    (@vaakash)

    Hi @khunmax,

    There are couple of things to note,

    1. Shortcoder does not execute “PHP” when it is pasted as “shortcode content”. Only HTML/JavaScript” is allowed. Even if you use PHP inside, it will be just printed as it is. You can use JS anyways to select the random banner.

    If you want a PHP only solution combined with shortcode, please refer this article to create a simple shortcode, you can integrate the above code with it.

    Shortcoder is really useful if you have lots of HTML/CSS/JavaScript snippets to use in your site in many places and when you cannot write PHP code like in the article for each and one of them.

    2. The above syntax error which you mentioned is an actual syntax error with the PHP code itself which you have written. I guess the quotes are not closed properly ?. Please do a google search on the syntax error to find a solution.

    Thanks,
    Aakash

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Inserting shortcode into woo page with hooks’ is closed to new replies.