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.
Related
I'm working on a Wordpress plugin that (amongst other things) renders a javascript podcast player in a post via a shortcode. What I did was to write the shortcode function in my plugin file to return the script tags as needed by the player – see below.
This worked quite well but my plugin was rejected by the review team with the comment that I should use the built-in Wordpress functions to enqueue scripts. But whatever I try, I don't manage to get a wp_enqueue_script working inside of my shortcode function. I also have tried pre-registering it – but either way, the javascript doesn't get loaded.
Does anybody know a solution for what I'm trying to achieve?
Update: Here's the piece of code, I'm struggeling with:
function podigee_player( $atts ) {
$atts = shortcode_atts(
array(
'url' => '',
),
$atts
);
return '<script class="podigee-podcast-player" src="https://cdn.podigee.com/podcast-player/javascripts/podigee-podcast-player.js" data-configuration="' . $atts['url'] . '/embed?context=external"></script>';
}
add_shortcode( 'podigee-player', 'podigee_player' );`
Okay cool, so I see what you're trying to achieve, and I also see why Wordpress would've blocked this.
An accepted way of enqueuing scripts is as below.
You need to state the script you wish to enqueue, and tell Wordpress where it needs to happen. In this instance, the script shall be added in the head, along with other default scripts.
The number I stated at the end is a version number, should you ever need to update it, which is doubtful since the script is external.
function podigee_external_scripts() {
wp_enqueue_script( 'podigee-podcast-player', 'https://cdn.podigee.com/podcast-player/javascripts/podigee-podcast-player.js', '20181023' );
}
add_action( 'wp_enqueue_scripts', 'podigee_external_scripts' );
I had the same problem as the OP, but Scott's answer didn't work for me, either.
My solution was, rather than calling:
add_action( 'wp_enqueue_scripts', 'podigee_external_scripts' ); // Not working.
I just call my function directly:
podigee_external_scripts(); // Your function that calls wp_enqueue_script(...)
I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?
Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.
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
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');
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 :)