- This topic has 9 replies, 3 voices, and was last updated 4 years, 4 months ago by
David.
-
AuthorPosts
-
March 16, 2021 at 3:20 pm #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');
March 16, 2021 at 3:37 pm #1698072Elvin
StaffCustomer SupportHi 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.March 16, 2021 at 3:47 pm #1698080Butch 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?
March 16, 2021 at 4:25 pm #1698098Elvin
StaffCustomer SupportIn 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.
March 16, 2021 at 5:01 pm #1698123Butch 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’);March 16, 2021 at 5:38 pm #1698144Elvin
StaffCustomer SupportAside 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#L1326Hook 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.
March 16, 2021 at 6:28 pm #1698170Butch 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 RulesHowever, 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
March 17, 2021 at 7:23 am #1698992David
StaffCustomer SupportHi 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.
March 17, 2021 at 7:25 am #1698997Butch Pornebo
it does. the code parses the dom for h2 and returns a json schema
March 17, 2021 at 8:00 am #1699042David
StaffCustomer SupportThis 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');
-
AuthorPosts
- You must be logged in to reply to this topic.