Hot answers tagged

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 $...


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

Check this question How does wordpress restrict X-FRAME to sameorigin?. The questioner's issue was resolved by modifying his site's .htaccess file by adding the below line to it as his Web Host set the X-Frame-Option. Header always unset X-Frame-Options You can check if that works for you.


3

For anyone wondering, as custom excerpts don't get trimmed by the excerpt_length filter hook, try adding this filter: function trim_custom_excerpt($excerpt) { if (has_excerpt()) { $excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 55)); } return $excerpt; } add_filter("the_excerpt", "trim_custom_excerpt", 999);


3

getBlockParents will work accurately. You can use getBlockParents with the clientId of the block, getBlockParents will return the all parent blocks id if the current block is under any Blocks. It will return blank if current block is not under any Block here is a method that you can use: const innerBlock = "namespace/block-name"; const parentBlocks = wp....


3

So if i understand you correctly, you want to sort by a value that has to be calculated based on the currency? You can do this, but not within a wordpress wp_query of sorts. Instead you can do one of two things: Method 1: The "do more now, have less hassle later"-way: On saving the currency and price in your save_post action, you calculate the AED-price ...


2

In cases like this the Wordpress adds a hash (unique id) to the function name and stores it in the global $wp_filter variable. So if you use remove_filter function nothing will happen. Even if you add the class name to the function name like remove_filter('plugins_loaded', ['MyClass', 'my_action']). All you can is to remove all the my_action hooks from the ...


2

The remove_filter() is necessary to avoid the callback from being called recursively, which would result in a memory issue due to a never-ending function execution. And that recursion could happen because the callback is hooked to get_post_metadata which is invoked via the get_metadata() function which the callback calls. So you need to first "unhook"/...


2

Use the render_block filter hook: add_filter( 'render_block', 'wrap_my_gallery_block', 10, 2 ); function wrap_my_gallery_block( $block_content, $block ) { if ( 'core/gallery' !== $block['blockName'] ) { return $block_content; } $return = 'my-gallery-block<div class="my-gallery-block">'; $return .= $block_content; ...


2

That's easy, pre_get_posts applies to all queries, not just those on the frontend. So if you don't want it to run on admin queries, test if you're in the admin and exit early! You might also want to verify that you're in the main query add_filter( 'pre_get_posts', function( \WP_Query $query) { if ( is_admin() ) { return; } if ( ! $query-...


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

This can be done using the blocks.getSaveElement filter as shown in the example below. I would STRONGLY caution that this has the potential for bad times. Core blocks are static blocks. This means that the output is saved to the database. If you want to remove these spans, you'll need to basically redo all of them. If the theme/plugin that introduces this is ...


2

If you just want to write a single <option> for the current user, you can get the user object from wp_get_current_user() - you don't need a WP_User_Query at all: function getŠ”urrentUserForFilter() { if ( is_user_logged_in() ) { $user = wp_get_current_user(); echo '<option value="'.$user->ID.'">'.esc_html($user->...


2

No this isn't possible without changing the original code. Filters work on the very first parameter, the other parameters are provided for context. You will need too either get the author to add a filter for the data variable, fork the plugin, or use a competitor. The only exception, is if $data is an object not an array and you want to modify it. However, ...


2

You can't replace the function, but you can substitute it: Copy the function to your child theme's functions file. Rename the function. Modify it as needed. Find the template file that references it, such as header.php, and copy that to your child theme. In your child theme's new template file replace the reference to the original function with your renamed ...


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

The best way to deal with core block frontend issues are "override core blocks rendering" example : <?php function foo_gallery_render( $attributes, $content ) { /** * Here you find an array with the ids of all * the images that are in your gallery. * * for example: * $attributes = [ * "ids" => [ 12, 34, 56, 78 ...


1

Try this function filter_image_sizes() { foreach ( get_intermediate_image_sizes() as $size ) { if (in_array( $size, array('medium', 'medium_large', 'large') ) ) { remove_image_size( $size ); } } } add_action('init', 'filter_image_sizes');


1

So after some digging managed to find the asnwer and instead of removing the filter you can overrid the single_template filter to add your own custom template. add_filter( 'single_template', 'my_custom_single_template', 99, 1 ); function my_custom_single_template( $single ) { global $post; if ( isset( $post ) && $post->post_type == '...


1

If I understand the question, you'd like the code to detect the service area that the page is addressing, and place in the relevant tagline/phrase under the logo, on that page. Presuming you are able to modify the php code for the page header template section that displays the logo and tagline, you could set up code to echo the desired tagline under the ...


1

I haven't played too much with Gutenberg, but I'll most likely need to solve something similar in the future, What i'd do, is just edit the function directly, for a quick debug. const rawTransforms = getRawTransformations(); const phrasingContentSchema = getPhrasingContentSchema( 'paste' ); const blockContentSchema = getBlockContentSchema( rawTransforms, ...


1

Looking at the related source in /wp-includes/category-template.php, we can see that there is no direct way of adding data attributes. /** * Filters the data used to generate the tag cloud. * * @since 4.3.0 * * @param array $tags_data An array of term data for term used to generate the tag cloud. */ $tags_data = apply_filters( '...


1

Theres no filter for that function. You can find the code in wp_includes/post.php (Obviously don't edit it there). Core File


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

You are able to change texts using a translation file. By default, plugin texts are in the English language. Even your site language is English, You are able to generate a new language file and change texts on it. Imagine that the plugin folder name (plugin slug, plugin text domain) is my-plugin and Your site language is set to English (UK) in Admin ...


1

This will only execute that code for a specific post type: function my_super_filer_function6($query_args){ global $post; if($post->post_type == 'specific post type'){ $post_author = $post->post_author; $query_args['author'] = $post_author; } return $query_args; } add_filter('listing/grid/posts-query-args', '...


1

There's a couple ways you could handle this. Option 1 Check the global $post object Inside your filter you may be able to check the current $post object or other Conditional Tags for a specific type. function my_super_filer_function6( $args ) { global $post; /* Check against the current post object */ if( empty( $post ) || 'post_type_here' !=...


1

The first block you provided can be sufficiently updated to be used with your code but see my example below: Start of by setting up your WordPress function to load your scripts (inside functions.php) and set the global ajax script variable to use later. I am breaking it down into sections since this would the correct way of doing it: function ...


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