Hot answers tagged

6

You can use the allowed_block_types hook: <?php function wpse_allowed_block_types($allowed_block_types, $post) { // Limit blocks in 'post' post type if($post->post_type == 'post') { // Return an array containing the allowed block types return array( 'core/paragraph', 'core/heading' ); } //...


5

There's a fundamental misunderstanding here about how filters and actions work, here is the flaw in your thinking: I know that, as a matter of fact, three arguments are passed in A closer inspection of add_action and add_filter reveals: add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ) Notice the $...


4

It's possible to avoid SQL execution in WP_Query with the posts_pre_query filter. It's e.g. used by plugins to outsource the default search to 3rd party search engines. Here's an example how we can override the main search query on the front-end, with an empty posts array and avoid running the SQL search query: add_filter( 'posts_pre_query', function( $...


3

Answer by @Pieter Goosen : If you need to update the page before the main query fires and returns the page object, you will manually need to parse the URL (probably on init) and get the page ID from get_page_by_title() or get_page_by_path(). Otherwise, 'wp' would be earliest hook to get the page ID, for example: function my_early_id() { $post = ...


3

Two inherent problems with your script as posted. I'm not certain this will solve your problem, but it's too big to address in the comments. First, you need to use add_action(), not add_filter(). That by itself is not a huge deal because add_action() is just a wrapper for add_filter(), but you should still use the correct one. The other problem may be your ...


3

Ignoring our off-topic request for a plugin, the good news is: yes, this is possible. On the other hand, it is quite some work, so I will give just an outline here. WordPress does have built-in image manipulation. The default one is Image Magick. WordPress' image editor class, however, only offers a subset of what Image Magick is actually capable of. This ...


3

The parent theme's functions.php runs after the child theme's, so in order to remove an action defined by the parent theme the remove_action call must be delayed using a hook after the parent theme registers the action. So putting the remove_action call purely inside the child's functions.php won't work. It must be attached to a hook. However from the code ...


2

Try this: I hope, It will works. Explaination: In below code in "is_page( 42 )" where 42 is page id of about page. So, if about page's id will be 42 then it's enqueue the stylesheet for that page only. Also I define particular path for this CSS file by using wordpress function get_template_directory_uri(). function testimonial_style() { if ( is_page( ...


2

Checking the action hooks API reference, "activated_plugin" is an Advanced hook. It operates outside the normal loop which would permit you to do what you want. A better way would be to add_action('admin_footer_text'), as illustrated in this WPBeginner article. However, if you only want this to show when a particular plugin is activated, you can add the ...


2

For anyone protecting their (development) site from public access, HTTP Authentication can be the cause of WP Cron not functioning. In case it can help anyone, here is my list of things I did before identifying and understanding the requirements of WP Cron: I noticed events were correctly scheduled and could be run using WP-CLI. And also noticed that ...


2

A nice clean way of doing is using adding a custom endpoint to wordpress REST API https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ function stripe_task() { include_once ABSPATH . 'path/to/stripe/autoloader/or/init'; //change $payload = @file_get_contents('php://input'); $event = null; try { $...


2

Adding inline CSS on an element with style=".." is bad practice, people say to use a CSS rule in a stylesheet instead, via a HTML class. So it doesn't make much sense to start styling things manually via javascript like that. Instead, wouldn't it make more sense to add a stylesheet that hides the title, then shows it when a CSS class is added to the main ...


2

WordPress is a whole package of different code components working together to provide a functional Content Management System. WordPress Core, Plugins, and Themes are all part of this package. Since some of these components update separately you do not want to edit the code directly, otherwise your changes will be overwritten whenever these components update. ...


2

For attachment posts, the following should do it, i.e. use the wp_insert_attachment_data hook along with get_post(): function my_random_post_id( $data, $postarr ) { // Runs if the post is being created and not updated. if ( empty( $postarr['ID'] ) ) { // Locate a yet-unused ID in the (wp_)posts table. do { // Based on ...


2

You can't, WP doesn't generate that markup, Google Chrome/Chromium does. There is no way to influence this from the server via PHP or JS or any other technology. It's just how browsers display images on their own directly. As far as the web server is concerned, WP isn't even loaded, and no HTML is sent, just the image data. Firefox does something similar, ...


2

No, for this to work reliably you would need to register hooks, which can't be done. The only way to know if a template triggers a hook is to load the template and find out. There is a solution to your problem, but testing if a hook is available is not that solution.


1

You can't enqueue because wp_loaded is too early. To enqueue scripts and styles, you need to do it on the wp_enqueue_scripts type hooks, but because your exiting on wp_loaded, none of those hooks have fired yet. Additionally, you need to have the necessary function calls in the template for it to enqueue them into, such as wp_head or wp_footer, otherwise ...


1

No, constructors should not define hooks. Constructors should be used to set the initial state of the object. Hooks have nothing to do with the object's initial state, so they don't belong in the constructor. Constructors should not have "side effects", so that using new ClassName() does not affect any other parts of the program, which is what registering ...


1

Unfortunately there doesn't seem to be any action hooks available for adding custom checkboxes to the Menu Settings section (Github/wp-admin/nav-menus.php). For registering menu locations, you should use register_nav_menus().


1

You can't just filter __FILE__. Or any arbitrary function or variable. You can only filter values that are passed to apply_filters(). In this case the wcpv_vendor_order_page_template filterable value is: dirname( __FILE__ ) . '/views/html-vendor-order-page.php' In other words, it's a path to a PHP file. If you want to change the PHP file that's loaded, you ...


1

You can use this filter, function ar_lazyload_deactivate() { if ( is_singular( 'posttype name' ) ) { add_filter( 'do_rocket_lazyload', '__return_false' ); } } add_filter( 'wp', __NAMESPACE__ . '\ar_lazyload_deactivate' );


1

save_post and new_to_publish is enough, with some checks, to update post metadata. And you don't need the redirection. <?php /** * Update Postmeta. * * @param integer $post_id Post ID. */ function wpse355298_job_publish_status( $post_id ) { // Check autosave. if ( wp_is_post_autosave( $post_id ) ) { return $post_id; } // ...


1

Not really, but perhaps you using the wrong terms: A shortcode is a manually entered piece of text where code has been programmatically added so that when wp runs 'the_content' filter , it'll then call the function and replace the piece of text with the returned result of the function. It's a specialised form of a filter. It sounds like what you want is ...


1

I realize you've already answered your issue. However, I think there is some additional explanation possible to give more clarity to the issue and the solution. The issue is your lqdnotes_add_div() function. This is hooked to a filter - the_content. In WordPress, any time you use a filter, your filter function must return a value for the item being ...


1

Choosing a higher priority for both the script AND the styles seems to work: add_action('wp_head', 'print_emoji_detection_script', 8); add_action('wp_print_styles', 'print_emoji_styles', 8);


1

It's still unclear to me why this is happening, because all the encoding of < and > for comments looks the same as posts do in the database view, so I'm still unsure why TinyMCE is treating post_content vs comment_content differently. But ultimately, all that needs to happen is for the existing escaped HTML tags to get decoded when editing on the ...


1

As mentioned on other answers, passing a parameter to the callback function is not possible by default. OOP and PHP anonymous function are workarounds BUT: Your code might not be OOP You might need to remove that filter afterwards If that is your case, there is another workaround for you to use: make yourself use of the add_filter and apply_filters ...


1

Since this isn't getting much action I'll put our band-aid fix as the current solution. Problem 1 Solution The added item meta data shows on the order confirmation page and does not show on the confirmation email. We solved this by utilizing the woocommerce_order_item_meta_end hook to add the extra item meta. Problem 2 Solution Adding the item meta data ...


1

There are a lot of fundamental mistakes here that suggest a misunderstanding of what filters are, and how they work. Filters are a way for you to intercept and modify a value. For example: add_filter( 'my_number', function( $value ) { return $value + 1; } ); $stuff = apply_filters( 'my_number' , 5 ); echo $stuff; // outputs 6 Here, we have the number 5, ...


1

Is this a sequence mismatch between the hooks? I would argue so, yes. acf/init gets executed by class ACF's init method, all in the root file acf.php - this method is hooked to WordPress' init with a priority of 5 like so add_action( 'init', array($this, 'init'), 5 ); And you probably add your CPTs with a higher priority, this means acf/init will be ...


Only top voted, non community-wiki answers of a minimum length are eligible