The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

7

When WordPress inserts a post, it runs the title through a filter called sanitize_title to get the slug. By default there is a function called santize_title_with_dashes attached to this filter with priority 10. This function simply strips out the |. If it is surrounded by spaces those spaces will be converted to hyphens. So your task is to run a filter on ...


6

Changing the "Post reverted to draft." text In WordPress version 5.0, WordPress introduced a new post type label which has the key item_reverted_to_draft and it's defined as: item_reverted_to_draft – Label used when an item is switched to a draft. Default is ‘Post reverted to draft.’ / ‘Page reverted to draft.’ Hence, as with any other post type ...


6

It depends on you where you want to call apply_filters(). It can be right when you defined the $title: $title = apply_filters( 'my_hook', the_title_attribute( 'echo=0' ) ); // set echo to false return $title; Or in the return call: $title = the_title_attribute( 'echo=0' ); // set echo to false return apply_filters( 'my_hook', $title ); Then one can hook ...


5

Yes, you can disable it. You can do this with code If you want to disable it globally, you can use this code: if ( version_compare($GLOBALS['wp_version'], '5.0-beta', '>') ) { // WP > 5 beta add_filter( 'use_block_editor_for_post_type', '__return_false', 100 ); } else { // WP < 5 beta add_filter( 'gutenberg_can_edit_post_type', '...


3

The following answer is a solution for Bootstrap 4 and centering images in a <figure> tag. WordPress generated default: <figure class="wp-block-image"> <img src="http://localhost:8888/time.png" alt="alt text" class="wp-image-1391"/> <figcaption>This is an image test.</figcaption> </figure> Using the preg_replace(...


3

How to use the filter single_product_archive_thumbnail_size I've searched for the tag single_product_archive_thumbnail_size in woocommerce repository on github. There is only the single occurrence of it as you see here. So how do we use it? The author's of WooCommerce added this filter so that other developers would be able to modify the product's ...


3

OK, so there are some occurrences of get_avatar() in your site. If you'll take a look at docs for this function, you'll see, that: you pass URL for default avatar image as 3rd param, you pass args as 5th param. And one of these args is: force_default (bool) (optional) Whether to always show the default image, never the Gravatar. Default: false. And ...


3

apply_filters and do_action are NOT stored in $wp_filter, ONLY add_filter and add_action are stored in $wp_filter When you call apply_filters or do_action, core WordPress loops through all of the registered filters or actions (added by add_filter and add_action), looking for any matching ones, and then executes the associated function. The only difference ...


3

Solution 1 I haven't tested the plugin, but based on this code from the link in your question: apply_filters( 'ep_prepare_meta_whitelist_key', false, $key, $post ) You can do something like this to explicitly allow a protected/private meta key: add_filter( 'ep_prepare_meta_whitelist_key', function( $allow, $meta_key ){ $meta_keys = ['_one', '_two', '...


3

Try this code function wrap_heading_with_div( $content ) { $heading = '/<h\d.*?>(.*?)<\/h\d>/ims'; $wrapper = '<div class="title">$0</div>'; $content = preg_replace($heading, $wrapper, $content); return $content; } add_filter( 'the_content', 'wrap_heading_with_div' ); Live Demo


3

When you add functions to the same handle without specifying the priority, they are executed in the order of addition. Every time you try to send an email, all the hooked functions will be called in the order in which they were added. To turn on email sending while a function runs, you can: remove __return_false from filter at the beginning of the function ...


3

post_type_link is a filter that is applied to the URL for a post before it’s returned by get_post_permalink. So it doesn’t change any rewrite rules and the structure of permalinks is still the same. So yeah - if you use it to change the structure of links, you have also to change the rewrite rules in such way, that the new link is matched and processed ...


3

You can use a static variable to achieve this: add_filter( 'the_content', 'asdf_the_content', 99, 1 ); function asdf_the_content( $content ) { static $has_run = false; if ( $has_run ) { return $content; } $has_run = true; // check if the_content has already been // filtered by some other function $content = ...


2

you can call getBlockHierarchyRootClientId with the clientId of the block, getBlockHierarchyRootClientId will return the parent block id if the current block is inside innerBlocks and will return the same id if it's root you can call it like this wp.data.select( 'core/editor' ).getBlockHierarchyRootClientId( clientId ); additionally, you can define a ...


2

When an action is added using a specific instance of a class (when you see $this), to remove the action you need to pass the same instance of the class to remove_action(). Since WC_Paid_Listings_Orders is a Singleton (it appears), there is only one instance of the class, and you can get that instance using the get_instance() method. You can then use that ...


2

I can't really make sense of what you're trying to do. This code: apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 ); Will let developers hook into the cc_get_content_preview hook to change the value that the cc_get_content_preview() function returns. When developers hook into an action or filter, they do so using a callback function. This ...


2

The reason you see the default value is that you are adding the filter after the filter has been applied, which results in the default value being used. The filter is applied when the object is constructed, which I'm assuming is before admin_init. If it's after admin_init it won't work either, but that's because the admin_init action has already fired. Here'...


2

When you call add_filter(), set the fourth parameter to 3 (which is the number of parameters accepted by the callback function which in your case is change_rating_output()), and then change your change_rating_output() function so that it accepts the $rating and $count parameters: add_filter('woocommerce_product_get_rating_html', 'change_rating_output', 10, ...


2

I just added this in my function.php and it works great add_filter('use_block_editor_for_post', '__return_false');


2

Generally the answer is no. Gutenberg stores content in a different format than how the pre 5.0 editor did. YMMV significantly if you try to disable gutenberg after content was already created, things might work for core blocks, but blocks created by plugins, who knows. Now if the question is about disabling before having any content edited in gutenberg, ...


2

Is it possible to do this with a rest query + filters? eg. http://wpsite.com/wp-json/wp/v2/posts?filter[only-permalinks] Yes, since 4.9.8 (see #43874) it's possible to render only fields needed with the _fields parameter. Examples Render only the permalinks: https://example.com/wp-json/wp/v2/posts?_fields=link [ { link: "https://example....


2

I don't believe there is a default endpoint to let you get all the posts and return only the permalinks. You could use the posts endpoint to get large chunks of permalinks (I don't believe it will let you return all of them in a single request) and paginate the request. To get it to return only the permalinks you could use the json_prepare_post filter all ...


2

There's a second argument passed to the_title filter, which is the ID of the post the filter is currently operating on. This is important, as you've discovered, because the filter runs any time a title is output- in a menu item, widget, any secondary query, etc., so you need to check if that post ID matches the ID of the post you're currently viewing. Your ...


2

It all depends, how these categories are displayed in your theme. If it's done nicely and with proper use of WP template tags, then that list comes from get_the_category() function (all other functions are using this one). And at the end of that function you can find return apply_filters( 'get_the_categories', $categories, $id ); So it's great news, ...


2

After some digging and trial/error I have came up with a couple solutions. I was looking for a "Gutenberg" solution so I could avoid using str_replace. First, we need to enqueue our JS and include the wp.blocks package // Add to functions.php function gutenberg_enqueue() { wp_enqueue_script( 'myguten-script', // ...


2

If you look at the gallery shortcode function the opening div (to match that closer you're missing) looks like this: $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; then this filter is run $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); More than ...


2

You don't need to write your custom Walker for that... Let's take a look on built-in Walker_Nav_Menu. You'll find this function: public function start_lvl( &$output, $depth = 0, $args = array() ) { if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { $t = ''; $n = ''; } else { $t = "\t"...


2

If you currently are using shortcodes, then most probably you want to display some dynamic content at the bottom of the post. If so, then there is a hook that will allow you to do that: the_content If you want to modify the content only when single post is displayed, then you can use this code: function my_the_content_filter( $content ) { if ( ...


2

I'm not able to test it right now, so I won't guarantee that it will work, but... 1. post reverted to draft When you grep through WP code searching for that string, only one occurrence appears. It's inside wp-includes/post.php file (see on trac) in get_post_type_labels function. And there is a filter used inside that function: post_type_labels_{$post_type}....


2

In recent versions of WordPress (5.0.1 or higher) file upload security has been tightened by actually checking the content of an uploaded file using the PHP function finfo_file() to determine if the file extension matches the MIME type. This has proven problematic since files with the same file type and extension may yield different MIME types. In my case ...


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