Archived topic
PHP with new Elements->Hook
3 replies · Started by Inna on June 10, 2019
Hello,
I'm trying to bring most of my PHP code from one of the plugins I have used before to the new Elements->Hook which as I understood can execute PHP.
The code I'm playing with is simple JetPack "Related posts" removal which works perfectly using the old plugin to execute php but doesn't work with Elements->Hook.
<?php
function jetpackme_remove_rp() {
if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
$jprp = Jetpack_RelatedPosts::init();
$callback = array( $jprp, 'filter_add_target_to_dom' );
remove_filter( 'the_content', $callback, 40 );
}
}
add_action( 'wp', 'jetpackme_remove_rp', 20 );
?>
The Hook I'm creating has options like:
Hook: before_header
Execute Shortcodes: On
Execute PHP: On
Priority: 0 or 100 (doesn't work with both)
Display rules Location: Entire Site
But the code doesn't work. What am I doing wrong? Btw I don't insert this hook as shortcode in to posts or pages. I'm thinking if Location is set to: Entire Site the code should run automatically?
Thank you,
Inna.
Hi there,
so Hooks come in two varieties - Action ( add_action ) and Filter ( add_filter ). The code you have is already hooking the function into the WP hook. So this cannot be added inside another hook.
That code has to be added to your theme function file or via the Code Snippets plugin.
Thank you for explanation. Now, if I remove the function call and put just the code in to gp hook it works.
if ( class_exists( 'Jetpack_RelatedPosts' ) ) {
$jprp = Jetpack_RelatedPosts::init();
$callback = array( $jprp, 'filter_add_target_to_dom' );
remove_filter( 'the_content', $callback, 40 );
}
Thank you ). Happy now.
Awesome - glad to be of help.