I’d been using the following code without error up until 1.9.3
I’d reverted back to 1.9.1 and it works as intended.
Had something changed in regards to how template shortcodes are called? Since 1.9.3, the blog posts this code applies to returns a 503 error.
I’d tested a plugin alternative (Ad Inserter – Ad Manager & AdSense Ads – WordPress plugin | WordPress.org) however inserting the template shortcode produces the same 503 error.
if ( ! function_exists( 'add_shortcode_after_paragraph' ) ) {
function add_shortcode_after_paragraph( $content ) {
// Define the custom post types where you want to add the shortcode
$allowed_post_types = array( 'post' ); // Only for blog posts
// Check if the current page is a single post and if the post type is allowed
if ( is_single() && in_array( get_post_type(), $allowed_post_types ) ) {
// Define the first shortcode to insert (The Bricks section display settings for the this shortcode must be set to only display on mobile device size.)
$first_shortcode = '[bricks_template id="706"]';
// Define the second shortcode to insert (The second CTA shortcode is intended to display on desktop & mobile.)
$second_shortcode = '[bricks_template id="809"]';
// Split the content into paragraphs
$paragraphs = explode( "</p>", $content );
// Initialize a counter to keep track of paragraph number
$counter = 0;
// Loop through the paragraphs to find the right spots to insert the shortcodes
foreach ( $paragraphs as $index => $paragraph ) {
if ( trim( $paragraph ) ) {
$counter++;
// Insert the first shortcode after the 1st paragraph
if ( $counter == 1 ) {
$paragraphs[ $index ] .= $first_shortcode;
}
// Insert the second shortcode after the 7th paragraph
if ( $counter == 7 ) {
$paragraphs[ $index ] .= $second_shortcode;
}
}
}
// Join the modified paragraphs back together
$content = implode( "</p>", $paragraphs );
}
return $content;
}
// Add the filter to apply the function to the content
add_filter( 'the_content', 'add_shortcode_after_paragraph' );
}