- This topic has 10 replies, 3 voices, and was last updated 3 years, 7 months ago by Tom.
-
AuthorPosts
-
August 26, 2020 at 8:52 pm #1419839Patrick
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.
August 27, 2020 at 4:05 am #1420136DavidStaffCustomer SupportHi 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(); } );
August 27, 2020 at 4:21 pm #1421136PatrickThanks! 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?
August 28, 2020 at 3:23 am #1421558DavidStaffCustomer SupportCode 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.
August 28, 2020 at 1:24 pm #1422294PatrickThis 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.
August 28, 2020 at 2:52 pm #1422378TomLead DeveloperLead DeveloperAny chance you can show us the full function you’re adding?
What error messages are seeing, specifically?
Let us know ๐
August 28, 2020 at 3:55 pm #1422398PatrickHere’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;">➤</span> Fuzzy thinking and foggy focus?</p> <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">➤</span> Growing forgetfulness?</p> <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">➤</span> Shrinking ability to learn and problem-solve?</p> <p><span style="font-size: 0.8em; font-weight: bold; color: #994200;">➤</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 12All 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.
August 28, 2020 at 3:59 pm #1422399PatrickAdding 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.August 29, 2020 at 10:11 am #1423203TomLead DeveloperLead DeveloperIt 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(); } );
August 29, 2020 at 4:47 pm #1423443PatrickThat worked. Thanks, Tom.
August 30, 2020 at 9:45 am #1424098TomLead DeveloperLead DeveloperYou’re welcome ๐
-
AuthorPosts
- You must be logged in to reply to this topic.