Site logo

Archived topic

Shortcode in Elements

5 replies · Started by Giorgio on September 17, 2021

Viewing posts 1–6 of 6

Hi, I would to put this shortcode: [sociallocker id="1795"]prova[/sociallocker] inside the Hook elements in php code:

$guidaShort = do_shortcode('[sociallocker id=\"1795\"]prova[/sociallocker]');
$contentBanner= $guidaShort.'<br>

....

but when I load the page I don't see the shortcode in action, while if I put the shortcode by post editor, I see the regular shortcode on post.

Thanks for helping
Giorgio

Hi Ying, I try to activate Execute Shortcodes and Execute PHP but not working.
I use shortcode of Content Locker plugin (MyThemesShop)
This is entire code php in hook element:

<?php 
/* inserisce CTA ebook più articolo correlato al centro del post  */
function ri_adv_middle_content( $content ) {
        if( !is_single() )
            return $content;
    
	  $prova= "prova";
	
    $guidaShort = do_shortcode('[sociallocker id="1795"]'.$prova.'[/sociallocker]');

    $contentBanner= $guidaShort.' <br><div class="sg-content-upgrade--inner"><b><u>Guida Gratis</u>: Vuoi Scoprire 10 Segreti per fare una pizza fatta in casa buonissima???</b> <p style="margin-top:10px;">Davvero in pochi li conoscono, e molto difficilmente li riuscirai a trovare altrove.</p>
<div style="text-align:center;margin-top:-15px;">
<a class="more-link" href="https://www.facebook.com/groups/pizzafattaincasa" target="_blank" rel="noopener noreferrer">
		<b>Clicca qui e accedi al mio gruppo facebook dove scaricarli subito.</b></a>
		</div>
		</div>';
	
    global $post;
    $orig_post= $post;
    $tags = wp_get_post_categories($post->ID);
    $first_tag = $tags[0]->term_id;
    $second_tag = $tags[1]->term_id;
    $args= array(
        'categories__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'posts_per_page'=>2,
        'caller_get_posts'=>1,
        'orderby'=>'rand'
    );
    
    
  $my_query = new wp_query( $args );
 
if( $my_query->have_posts() ) {
    $my_query->the_post();
    $link= get_permalink($post->ID);
    $title= get_the_title($post->ID); 
    $image= get_the_post_thumbnail($post->ID,array(150,150));
    $contentRelatedPost= "<div><h3 style=\"margin-bottom: 10px;margin-top:50px;font-weight: 700;
    font-size: 20px;line-height: 1.7em;\">Non perdere anche:</h3></div><div class=\"inline-related-posts\"><a href=";
    $contentRelatedPost .= $link;
    $contentRelatedPost .= " rel=\"bookmark\">";
    $contentRelatedPost .= $image;
    $contentRelatedPost .= "<div class=\"titolo-inline-related\">";
    $contentRelatedPost .= $title;
    $contentRelatedPost .= "</a></div>";
    $contentRelatedPost .= "<div class=\"riassunto-related\">";
    $contentRelatedPost .= WPSEO_Meta::get_value("metadesc");
    $contentRelatedPost .= "<br><a class=\"more-link\" href=";
    $contentRelatedPost .= $link;
    $contentRelatedPost .= " >Leggi ora</a>";
    $contentRelatedPost .= "</div>";
    $contentRelatedPost .= "</div>";
    
    
    }
    
if( $my_query->have_posts() ) {
    $my_query->the_post();
    $link= get_permalink($post->ID);
    $title= get_the_title($post->ID); 
    $image= get_the_post_thumbnail($post->ID,array(150,150));
    $contentRelatedPostEnd= "<div><h3 style=\"margin-bottom: 10px;margin-top:50px;font-weight: 700;
    font-size: 20px;line-height: 1.7em;\">Non perdere anche:</h3></div><div class=\"inline-related-posts\"><a href=";
    $contentRelatedPostEnd .= $link;
    $contentRelatedPostEnd .= " rel=\"bookmark\">";
    $contentRelatedPostEnd .= $image;
    $contentRelatedPostEnd .= "<div class=\"titolo-inline-related\">";
    $contentRelatedPostEnd .= $title;
    $contentRelatedPostEnd .= "</a></div>";
    $contentRelatedPostEnd .= "<div class=\"riassunto-related\">";
    $contentRelatedPostEnd .= WPSEO_Meta::get_value("metadesc");
    $contentRelatedPostEnd .= "<br><a class=\"more-link\" href=";
    $contentRelatedPostEnd .= $link;
    $contentRelatedPostEnd .= " >Leggi ora</a>";
    $contentRelatedPostEnd .= "</div>";
    $contentRelatedPostEnd .= "</div>";
    }
     
    $post = $orig_post;
    wp_reset_query();

    $content = explode ( "</p>", $content );
    $paragraphAfter = count ( $content ) / 2 ;
    $paragraphAfter = (int)$paragraphAfter;
    
    $paragraphInlineRelatedPost = count ( $content ) / 3 ;
    $paragraphInlineRelatedPost = (int)$paragraphInlineRelatedPost;
       
    $paragraphInlineRelatedPostEnd = count ( $content ) - 5 ;
    $paragraphInlineRelatedPostEnd = (int)$paragraphInlineRelatedPostEnd;
    
    $new_content = '';
    
	
            for ( $i = 0; $i < count ( $content ); $i ++ ) {
         
              if ($i == $paragraphInlineRelatedPost){
                    
                    $new_content .= $contentRelatedPost;
                }
                    
                
                if ( $i == $paragraphAfter) {
                    
                    $new_content .= $contentBanner;
                 
                } 
                   

                 if ($i == $paragraphInlineRelatedPostEnd){
                    
                    $new_content .= $contentRelatedPostEnd;
                }
                    
            $new_content .= $content[$i] . "</p>";
                
            }
    
    return $new_content;
}

add_filter( 'the_content', 'ri_adv_middle_content' );
?>

Hi Giorgio,

I see you're doing a filter for the_content. You shouldn't use Hook elements for this. Hook elements are for presentation purposes only. It's not meant for filtering actual contents even though it runs PHP.

In this case, you should place this code on a child theme's functions.php or on a Code Snippets plugin.
https://docs.generatepress.com/article/adding-php/

Hi Elvin, I activated a child theme and put all the above php code there, but I don't see my shortcode in action.
Thanks for helping
Giorgio

So the shortcode isn't working in the post content or elements?

Then the function itself is likely the issue.

This archived topic is closed to new replies.