Misplaced PHP-Generated Elements

Hello there, I’ve got several sites that are on the latest available versions of the themes (X 9.0.8 and Pro 5.0.8). I’m having issues with some custom php functions appearing in the wrong place on my page (typically at the beginning of my .entry-content.content). These functions have all worked flawlessly until I updated to the latest version of the themes recently.

I noticed in the changelog preview of 9.1 and 5.1, it says there is a fix for "Bugfix: Fix unhandled exception when post_id is unacessible in some cases of plugins rendering the content outside of a normal theme template." Is this the fix for what I am experiencing?

Hello @bobbybosler,

Thanks for writing in! The error usually occurs when you have a customized template to display the content. Yes, it is correct. The next update is already now on a release candidate 2. This is why the changelog is already up. Please watch out for it when the stable release version rolls out anytime soon.

Best Regards.

This was occurring for me when I had a shortcode in a raw element. Changing it to a text element fixed the position so that could be a workaround.

Glad to hear that, Clint.

Hey @ruenel and @christian,

I wanted to give an update on this issue. The latest release (9.1.0) does not solve our problem here. It seems to have to do with custom shortcodes not functioning in certain elements. What I’ve found is that the following cornerstone elements do not handle shortcodes anymore (since at least 5.0.8): RAW elements, classic text elements, or Tab content. While I have had some success with replacing these with v2 text elements, that is not the final solution, I fear. This is a real problem for many of us who have customized our sites and I’m pretty sure a change in Cornerstone/Pro is the culprit.

Hi @bobbybosler,

I have checked it in the latest version and didn’t find the issue anymore. If you are still facing the issue, I would suggest you please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Thanks

@tristup,

Thanks for your reply. This issue may not be related to the changelog entry, but it is an issue nonetheless.I will post an example page here and then try to recreate a related issue on another website I maintain.

This page illustrates what is happening: https://fallsbaptist.org/sandbox/ Note that the shortcodes are displaying properly in Cornerstone, but they “jump” outside of their expected place in the DOM when viewing the front end of the page. This only happens in certain elements, as exemplified on the page.

I will post login info in a secure note here as well.

Hi @bobbybosler,

I have investigated the page and found the issue you described. I would suggest you create the Global Blocks and add the shortcode into it, then use the Global Blocks as a shortcode into the Tab. I have created a Global Block and added it to the Tab and it works.

Still, I will check it more and will add it to our issue tracker if anything is found.

Thanks

@tristup,

Thank you for checking into this and for the temporary workaround. I trust you’ll keep us up to date once this issue has been fully resolved.

Hi @bobbybosler,

I don’t think this is an issue with the theme, but rather how your shortcode is setup. I suspect your shortcode might be using echo or otherwise outputting PHP directly. Instead, it needs to return a string. WordPress has some more info on this in their documentation.

Hey @alexander,

I don’t expect you to debug my shortcode, but this shortcode has literally been working fine for six years until the 9.0.8 update. You could be right that there’s nothing wrong with the theme, but it was the update that made my shortcode only work under certain circumstances. This is the code that runs when the shortcode is called. If you can point out what I’m doing that’s now incompatible with X, I would much appreciate it!

 // Add latest sermon player shortcode
 function return_latest_am_sermon() {
 	// Setting up the Query - see http://codex.wordpress.org/Class_Reference/WP_Query
 $latest_sermon = new WP_Query(array(
 	'post_type' => 'wpfc_sermon',
 	'posts_per_page' => 1,
 	'post_status' => 'publish',
 	// Do you want to limit it to a specific service type? Use the service type slug to do it:
 	// More info here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
 	'wpfc_service_type' => 'sunday-am-series',
 	'order' => 'DESC',
 	'meta_key' => 'sermon_date',
         'meta_value' => date("m/d/Y"),
         'meta_compare' => '>=',
         'orderby' => 'meta_value',
 	// The last three parameters will optimize your query 
     'no_found_rows' => true,
     'update_post_term_cache' => false,
     'update_post_meta_cache' => false
 	));
 if ($latest_sermon->have_posts()) :
 ?>
 	<?php  while ($latest_sermon->have_posts()) : $latest_sermon->the_post(); ?>
 	<?php global $post; ?>
  
 <div class="latest-am-sermon">
 <a class="latest-sermon-link" title="<?php echo esc_attr( get_the_title() ); ?>" href="<?php the_permalink() ?     >"><?php render_sermon_image('full'); ?></a>
 <div class="latest-sermon-text">
 <h3 class="latest-sermon-title"><?php the_title(); ?></h3> 
 <h4 class="meta">
 <span class="speaker"> <?php the_terms( $post->ID, 'wpfc_preacher', '', ', ', ' ' );  ?>
 </span>
 </h4>
 </div>
 <div class="latest-sermon-download"> 
 <?php echo '<a href="' . get_wpfc_sermon_meta('sermon_audio') . '" class="sermon-attachments download-     link" title="Download MP3 of ' . esc_attr( get_the_title() ) . '" download>'.__( 'MP3', 'sermon-manager').'</a>'; ?>
 </div>
 <?php // this will output the media links ?> 
 <div class="latest-sermon-player"><?php wpfc_sermon_files(); ?></div>
 </div>
 <?php endwhile; ?>
 <?php // reset the $post variable like below: ?> 
 <?php wp_reset_postdata(); ?> 
 <?php endif;
 }
 add_shortcode( 'latest_am_sermon', 'return_latest_am_sermon' );

Hi @bobbybosler,

From the WordPress docs, this is specifically what I was referring to:

Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce unexpected side effects from the call since you cannot control when and where they are called from.

This is one of those unexpected results. I’m not quite sure what changed exactly, but to Cornerstone, it’s like the shortcode doesn’t provide any content at all. Instead, at the time the output was requested, it just gets output immediately.

PHP does let you turn “output” into a string though via what’s called output buffering. Here’s something that may help: https://wordpress.stackexchange.com/a/187860

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.