the_excerpt and the_content filters - wordpress

I'm in a kind of a bind. I'm adding rating to posts in WP. It's done with the_content filter. Thing is, some themes use excerpt instead of content in, say, archive loops. Adding rating to that is as simple as just adding filter for the_excerpt. Problem is, when excerpt is retrieved by theme, it fires the_content filter too (so rating is actually added), but after that, content is stripped off all html tags, so rating as it is (shapes) is gone but vote counter remains. This leads to non pretty situation like this:
Now I'm wondering what's the good way around it? I don't think there is a way to see list of actions which will call action handler for the current post (so that if action handler is called from the_content filter (check by current_filter()) and there is the_excerpt in 'queue' for this post just return content without changes) or a way to know to know if the_content was fired by function to retrieve excerpt. Of course, very dirty and horrible workaround would be to check content for vote counter text when action handler is fired by the_excerpt and just replace it with empty string but that's not a good solution. Am I missing something here? Is there a cleaner way of doing this?

Ok, the cleanest solution I could come up with is this
function remove_mah_filter($content)
{
if (has_filter( 'the_content', 'your_filter' ))
{
remove_filter( 'the_content', 'your_filter' ); // if this filter got priority different from 10 (default), you need to specify it
}
return $content;
}
add_filter('get_the_excerpt', 'remove_mah_filter', 9); //priority needs to be lower than that of wp_trim_excerpt, which has priority of 10. Otherwise, it will still be triggered for the first post in the loop.
// add it back so that it can be called by the actual content
function readd_mah_filter($content)
{
add_filter( 'the_content', 'your_filter' ); // if this filter got priority different from 10 (default), you need to specify it
return $content;
}
add_filter('get_the_excerpt', 'readd_mah_filter', 11); //priority needs to be higher than that of wp_trim_excerpt, which has priority of 10.

Related

storefront_sticky_add_to_cart refuses to be removed

Almost finished my WooCommerce Storefront child theme.
There's a block sitting under the footer on the product pages that I have zero desire to keep. Just as I've done with a few other extraneous pieces of markup, I tracked down the action and removed it:
remove_action( 'storefront_after_footer', 'storefront_sticky_single_add_to_cart');
... It is still showing in the page.
I've even tried some random things I found:
add_filter ('storefront_sticky_add_to_cart', '__return_false');
and
function cleanup_parent_filters()
{
//...
remove_action( 'storefront_after_footer', 'storefront_sticky_single_add_to_cart');
}
add_action( 'wp_loaded', 'cleanup_parent_filters');
With no results.
This is really more annoying than anything. I can remove the entire do_action( 'storefront_after_footer' ); from the template as I really just don't care to have it there, but now I need to know why this is not working as expected out of principle.
try
remove_action( 'storefront_after_footer',
'storefront_sticky_single_add_to_cart', 999 );
remove_action needs the priority value as it was used to add the action. From the Docs
Important: To remove a hook, the $function_to_remove and $priority
arguments must match when the hook was added. This goes for both
filters and actions. No warning will be given on removal failure.
And why 999? From the source code.
Disclaimer: I haven't tested this out.

How to safely remove an action hook in child-theme?

I am working on a WordPress installation called PointFinder. Items that are posted by users get deleted after some pre-defined period. My goal is to disable this behaviour. I already found the corresponding code lines and deactivated the add_action hook that triggers the periodically called function pointfinder_clean_pending_orders() in schedule-config.php, which works fine for the moment.
add_action( 'pointfinder_schedule_hooks_hourly', 'pointfinder_clean_pending_orders' ); // <--- commented out this line
function pointfinder_clean_pending_orders() {
/* code that cleans-up ... */
}
How can I achieve the deactivation in my child-theme ? If I simply add a remove_action to functions.php in my child-theme, will that work? I am not sure what will be called first, add_action in schedule-config.php or my remove_action in functions.php ?
I only have access to the productive server and no testing environment and thus I am a bit reluctant regarding experiments.
try this:
add_action('init','remove_hourly_hook');
function remove_hourly_hook() {
remove_action( 'pointfinder_schedule_hooks_hourly', 'pointfinder_clean_pending_orders' );
}
What this does is after WordPress is initialized (regardless of the order of functions.php being called), it will then remove the action.
Now, if the initial add_action is inside a its own hook, you will want to make sure your hook 'init' is changed to occur afterwards.

Wordpress re write rule

Lets say I have a URI thats portfolio_categories/guttersspouting/
How could I rewrite a function to change it too "products"
ok as requested this matches the uri "product" exactly. This is not advisable if releasing code to a third party, as if they create a page called product, it will follow this rule.
Btw I am assuming you mean post_type = page (will also work for posts, see the post_id= in the add_rewrite_rule? You need to change this to the post id of the page you want to redirect to.
add_action('init', 'new_rewrite_rule');
function new_rewrite_rule(){
// matches product exactly, product/aproduct will fail, etc
// you need to add your own post_id into the function below.
add_rewrite_rule('^product$','index.php?page_id=12','top');
}
You will need to flush the rewrite rules for this to work (go to the permalinks setting page and just hit the save button)
code version of flush rewrite rules, not a good idea to have it running on wp_loaded, should be on a activation hook really but for testing this will do:
function new_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'new_flush_rewrite_rules' );
Just to note you can also do this manually in the post edit screen.

remove custom meta boxes not working

what I was trying to do here is to remove some custom fields that I created when a template is selected, aka when I select certain template I want to hide or show specific metaboxes.
The code I have is the following but it isn't working at all (thats to say that it doesn't remove any metaboxes) and I would like help to see what's wrong with it or if what I'm trying to do it's just not posible.
add_action('admin_init','my_meta_init');
function my_meta_init(){
$template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);
if (($template_file == 'thanks-template.php') || ($template_file == 'view-template.php'))
{
remove_meta_box('my_meta_box_id','page','normal');
remove_meta_box('my_meta_box_id_2','page','side');
remove_meta_box('my_meta_box_id_3','page','side');
remove_meta_box('dynamic_sectionid','page','normal');
} else
{
remove_meta_box('my_meta_box_id_4','page','normal');
}
}
Thanks you for the comments and answer, everyone helped. The problem was on the hook I was using, I changed it and now it's working perfectly :
add_action('admin_head','my_meta_init');
You may need to change the HOOK you are using to hook in your function.
That is you need to hook into admin_menu instead of admin_init as the metaboxes might not exist the moment you are trying to remove them. So a certain order is needed to make sure metaboxes removal call is made when actual metaboxes are generated and exist.
I tested following code on my localhost and it hid the Author div/metabox fine when used this code snippet:
function remove_page_fields() {
remove_meta_box( 'authordiv' , 'page' , 'normal' ); //removes author
}
add_action( 'admin_menu' , 'remove_page_fields' );
Another Approach:
By the way, as I think about the situation you are facing, maybe add the metaboxes/custom fields in such a way, that they are shown only to the pages we are meant to. I have worked on projects where I need to show some metaboxes only when certain template is selected.
I use CMB2 class to generate metaboxes mostly, if you happen to use that or something similar, you may use this parameter to specify the page templates https://github.com/WebDevStudios/CMB2/wiki/Display-Options#limit-to-specific-page-templates

WordPress Thesis theme - hook below post

i need a little help with thesis theme, i made a simple hook for single post with custom content and i want the hook to be right after the post but after the post there's a plugins like facebook etc... and my hook is below that. How to make my hook to be right after post without disabling this plugins ?
*i already tried every posible combinations thesis_hook_after_post, thesis_hook_after_post_box etc and it dont work
Thanks in advance !
Change priority value in add_action
add_action( $tag, $function_to_add, $priority, $accepted_args );
(int) (optional) Used to specify the order in which the functions
associated with a particular action are executed. Lower numbers
correspond with earlier execution, and functions with the same
priority are executed in the order in which they were added to the
action. Default: 10
Edit:-
function post_article111() {
if (is_single( )) {
echo "content goes here..";
}
}
add_action('thesis_hook_after_post_box', 'post_article111','5');

Resources