WordPress Thesis theme - hook below post - wordpress

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');

Related

pre_get_posts not limiting posts per pages

I have set up the following in my functions.php to raise the limit of posts being shown when doing a public search:
if ( ! is_admin() && $query->is_search) {
$query->set('posts_per_page', 20);
}
}
add_action( 'pre_get_posts', 'search_results_query' );
But however much I tried to modify it, I don't seem to have any effect. What am I missing?
You are missing part of your code but do you have $query set to pass through your function?
function search_results_query( $query ) {
I just tested your code and set it to 1 using the 2019 theme and it worked fine with the above.
Thank you, the problem was that the theme had already set the posts per pages. I managed to change the number of posts from the appearance menu. The page template had already theme actions and hooks and I have a limited understanding yet of it all, but I think that was what was interfering with my hook. Or I just simply didn't understand fully how to hook it up properly. Either way, I resolved it.

the_excerpt and the_content filters

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.

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

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

How to filter Wordpress posts using a hook in a plugin?

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.
I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.
Also, due to the plugin nature, I think it make no sense to modify the theme's template.
I'm sure the solution is evident, but can't find it!
I thought there was a nicer solution, but this is how I finally solved it:
add_filter ( 'query_vars', 'myplugin_filter_posts');
function myplugin_filter_posts( $content )
{
//WP's query handler
global $wp_query;
//The id of the category whose posts I'd like to show
$catId = 1;
$result = $wp_query->query( 'cat='.$catId );
return $content;
}
If you tips for a better solution, please share :)

Resources