[Support request] Hook on wp_footer

Home Forums Support [Support request] Hook on wp_footer

Home Forums Support Hook on wp_footer

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1698065
    Butch Pornebo

    Using a test site, I have this hook in functions.php, and it’s executing as expected.

    I’m confused on how to adapt this by using GP Elements > Hook > wp_footer

    How do I adapt this code to work within GP Elements because I’m going to need to run this code on a specific post(s) only which I can easily control in the GP Elements exclusion options?

    function add_script_class() {
    	$matches = $jsoneContentArr = [];
    	$contentArr["@context"] = "http://schema.org/";
    	$contentArr["@type"] = "FAQPage";
       	//$url =  get_site_url() . $_SERVER[ 'REQUEST_URI' ];
    	//$current_post_id = url_to_postid($url);
    	//$res = get_post($current_post_id);
    	$query = get_post(get_the_ID()); 
    	$res = apply_filters('the_content', $query->post_content);
    	if(!empty($res)){
    		//$content = '<div>'.wpautop($res->post_content).'</div>';//die;
    	    $content = mb_convert_encoding($res, 'HTML-ENTITIES', "UTF-8");
    	    $dom = new DOMDocument();
    	    @$dom->loadHTML($content);
    	    
    	    
    	    foreach($dom->getElementsByTagName('h2') as $key=>$node) {
    	    	if(!empty($node)) {
    	    		
    	    		
    	    		if(!empty($node->firstChild) && $node->firstChild->tagName == "span") {
    	    			//print_r($node->firstChild->attributes->item(0)->nodeValue);
    			    	//$matches[$key]['h2Tag'] = $node->textContent;
    			    	$matches[$key]['h2Tag'] = $node->firstChild->attributes->item(0)->nodeValue;
    					$matches[$key]['h2TagMain'] = $node->textContent;
    			    	$dom->saveHtml($node);
    				    while(($node = $node->nextSibling) && $node->nodeName !== 'h2') {
    				        if($node->nodeName == 'p') {
    				            $matches[$key]['ptag'][] = $dom->saveHtml($node);   
    				        }
    				    }
    				}
    			}
    		}
    	}
    	if(!empty($matches) && count($matches) > 0){
    		foreach ($matches as $key => $value) {
    
    			$acceptedAnswer =   array('@type' => "Answer", 
    									"text" => strip_tags($value["ptag"][0])."<a href='#".str_replace(' ','-',$value['h2Tag'])."'> ... Read More</a>"
    								);
    			$jsoneContentArr[] = array(
    										"@type" => "Question",
    										"name" => $value['h2TagMain'],
    										"acceptedAnswer" => $acceptedAnswer
    									);
    		}
    	}
    	$contentArr["mainEntity"] =$jsoneContentArr;
        if ($jsoneContentArr) {
            ?>
    	<script id="myJSONID" type="application/ld+json"><?php echo json_encode($contentArr); ?></script>
    	<?php
        }
    }
    add_filter('wp_footer', 'add_script_class');
    #1698072
    Elvin
    Staff
    Customer Support

    Hi there,

    You shouldn’t use Hook Element for this kind of PHP snippet.

    Try this instead:

    Change this line of code:
    add_filter('wp_footer', 'add_script_class');

    To this:

    add_filter('wp_footer', function(){
        if ( is_single( array( '1', '2') ) ){
            add_script_class();
        }
    };

    This condition – is_single( array( '1', '2') ) – is for you to be able to select specific posts.
    https://developer.wordpress.org/reference/functions/is_single/

    Simply popular the array() with the post IDs you want this applied on.

    #1698080
    Butch Pornebo

    the PHP code is parsing H2 elements and echoing back a script JSON schema.

    In any case, I don’t want to hand-code the post id BUT rather use the GP Elements Display Rules to selectively choose the post I want to be included or excluded whatever the case may be.

    Are you saying that this code will not work in GP Element wp_footer hook?

    #1698098
    Elvin
    Staff
    Customer Support

    In any case, I don’t want to hand-code the post id BUT rather use the GP Elements Display Rules to selectively choose the post I want to be included or excluded whatever the case may be.

    Display rule location literally does the same thing as the code I’ve written.

    Are you saying that this code will not work in GP Element wp_footer hook?

    We generally don’t do add_filter() on Hook Elements as it’s for presentation purposes meaning it’s generally for hooking shortcodes or HTMLs in.

    While it can work, it can potentially cause issues.

    #1698123
    Butch Pornebo

    Display rule location literally does the same thing as the code I’ve written.

    But I have to find the post-id then type it in manually as opposed to Display Rules, it will present me a selection post. Less prone in selecting

    While it can work, it can potentially cause issues.

    I’ll cross that bridge when the time comes.

    Aside from deleting the following lines which did not actually work, what else I need to add to adapt my code?

    function add_script_class() {

    and

    }
    add_filter(‘wp_footer’, ‘add_script_class’);

    #1698144
    Elvin
    Staff
    Customer Support

    Aside from deleting the following lines which did not actually work, what else I need to add to adapt my code?

    Generally, if you want to run PHP snippets on the Hook Element, you’ll have to wrap the code with <?php .. ?> and make sure the Execute PHP option is checked.

    But now, after fully reading and absorbing what your code does, I don’t think this will work on the Hook element at all as this involves a filter for a WordPress core functionality.
    https://github.com/WordPress/WordPress/blob/4d514bb6ad0af5569113c5f679bee33e8e153d82/wp-includes/class-wp-customize-nav-menus.php#L1326

    Hook Elements are for Hooks(add_action).

    You should do this on code snippets or functions.php as you’ve been doing. As for the selective applying, the w/ the condition suggested in the previous reply is the way to go, unfortunately.

    #1698170
    Butch Pornebo

    I tried earlier using Code Snippets as a filter to wp_footer. It worked. That solved the issue of using functions.php but the issue of manually entering the post id will still be an issue.

    So, I thought:

    1) If I make this a shortcode, placing it on Code Snippets
    2) Using GP Elements wp_footer and only execute the shortcode via Display Rules

    However, can’t even get past #1. By just simply changing from add filter to add shortcode, I was getting 500 Internal Server Error when I tried to hardcode the shortcode in the post.

    I’m a programmer BUT very little knowledge of PHP.

    Do you have any advice on how to make it work as a shortcode?

    Thanks in advance

    #1698992
    David
    Staff
    Customer Support

    Hi there,

    Shortcodes should only be used to return content – see here:

    https://developer.wordpress.org/reference/functions/add_shortcode/

    Key point:

    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.

    #1698997
    Butch Pornebo

    it does. the code parses the dom for h2 and returns a json schema

    #1699042
    David
    Staff
    Customer Support

    This is what i mean by return:

    function simple_function($html) {
    	// Do some stuff
    	$html = 'the content output from doing some stuff here';
    	return $html;
    }
    
    add_shortcode('simple_shortcode', 'simple_function');
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.