[Resolved] Creating/converting shortcodes

Home Forums Support [Resolved] Creating/converting shortcodes

Home Forums Support Creating/converting shortcodes

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1419839
    Patrick

    I am trying to move from Dynamik Website Builder to GP Premium. My current issue is with shortcodes I use to display text throughout my site. I have spent an hour looking for an answer in the forum with no luck. I am working on a staging site.

    Sample shortcode: [about_us]
    Corresponding HTML: <p>code</p>

    I have created a hook in Elements, but I don’t think that is the right way to go.

    I have many shortcodes like this. I’m lost for ideas since the GP ecosystem is so different than Dynamik. Maybe a PHP function and a plugin would work? But how or why, I really don’t know.

    #1420136
    David
    Staff
    Customer Support

    Hi there,

    This article provides the code for creating shortcodes:

    https://docs.generatepress.com/article/creating-a-shortcode/

    Here’s an example:

    add_shortcode( 'about_us', function() {
        ob_start();
        // Start your PHP below
      
        echo '<p>About us content</p>';
      
        // End your PHP above
        return ob_get_clean();
    } );
    #1421136
    Patrick

    Thanks! I used the Code Snippets plugin to add your PHP code. Is that the best practice?

    The content code I enter with the PHP has some Google Analytics tracking code, ie, onclick=”ga(‘send’, ‘event’, …);”. This sets off all kinds of error messages. How can I use the tracking code properly?

    #1421558
    David
    Staff
    Customer Support

    Code Snippets is fine. Unless you’re using a Child Theme, then you can those codes your child functions.php

    Do you have an example of the full code you’re trying to add – might see something.

    #1422294
    Patrick

    This is the piece of code causing problems:
    onclick=”ga(‘send’, ‘event’, {eventCategory: ‘Promotional’, eventAction: ‘Block ad in post’, eventLabel: ‘Mind Lab Pro’});”

    All the errors I see pertain to this code (I’m testing on a staging site right now). This code causes no problems in the shortcode on the live site.

    #1422378
    Tom
    Lead Developer
    Lead Developer

    Any chance you can show us the full function you’re adding?

    What error messages are seeing, specifically?

    Let us know ๐Ÿ™‚

    #1422398
    Patrick

    Here’s the full code:

    <!-- Post1, Mind Lab Pro -->
    <div class="insidepostad">
    <img src="https://example.com/wp-content/uploads/2020/03/woman-watering-flower.jpg" alt="woman watering flower springing from her head" width="200" height="133" />
    <p style="text-align: center; font-weight: bold; font-family: arial, helvetica, sans-serif; font-size: 20px;">WHAT CAN A BRAIN SUPPLEMENT DO?<br><span style="font-weight: normal; font-style: italic; font-family: Georgia, serif;">Are you struggling with ...</span></p>
    <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">&#x27A4;</span> Fuzzy thinking and foggy focus?</p>
    <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">&#x27A4;</span> Growing forgetfulness?</p>
    <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">&#x27A4;</span> Shrinking ability to learn and problem-solve?</p>
    <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">&#x27A4;</span> Lack of mental energy and drive?</p>
    <p style="font-weight: bold;">Mind Lab Pro can help your brain perform better.</p>
    <p style="font-weight: bold;"><a href="https://example.com/mind-lab-pro-review/">REVIEW: Why I recommend Mind Lab Pro for better mental performance.</a></p>
    <p><em><strong>Dr. Pat</strong></em></p>
    </div>

    As for error messages, I can’t copy them. There are 10 messages shown with a pink background when I hover over the red X on line 12.
    Here are 2 examples:
    Parse Error : syntax error, unexpected ‘Mind’ (T_STRING), expecting ‘;’ on line 12
    Parse Error : syntax error, unexpected ”,” (T_CONSTANT_ENCAPSED_STRING), expecting ‘;’ on line 12

    All the T_STRING errors are very similar; the only difference is the value that follows “unexpected.” All the T_CONSTANT_ENCAPSED_STRING errors are identical.

    All this is occurring within the Code Snippets plugin.

    #1422399
    Patrick

    Adding to the previous reply:
    That’s interesting. The code that I entered is not the code you see. In particular, the G Analytics tracking code is stripped. Also anything that does not begin with a paragraph tag. I assume you do this for security.

    #1423203
    Tom
    Lead Developer
    Lead Developer

    It sounds like the PHP syntax is breaking. Make sure your shortcode function looks like this:

    add_shortcode( 'your_shortcode_name', function() {
        ob_start();
        ?>
      
        Add your HTML here.
      
        <?php
        return ob_get_clean();
    } );
    #1423443
    Patrick

    That worked. Thanks, Tom.

    #1424098
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

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