Hot answers tagged

194

We will hook into init and remove actions as followed: function disable_wp_emojicons() { // all actions related to emojis remove_action( 'admin_print_styles', 'print_emoji_styles' ); remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( '...


82

By default this is not possible. There are workarounds if you do it the OOP way. You could create a class to store the values you want to use later. Example: /** * Stores a value and calls any existing function with this value. */ class WPSE_Filter_Storage { /** * Filled by __construct(). Used by __call(). * * @type mixed Any type you ...


80

Whenever a plugin creates a new MyClass();, it should assign it to a uniquely named variable. That way, the instance of the class is accessible. So if he was doing $myclass = new MyClass();, then you could do this: global $myclass; remove_action( 'wp_footer', array( $myclass, 'my_action' ) ); This works because plugins are included in the global namespace,...


80

That’s a very good question. It goes down to the dark heart of the plugin API and best programming practices. For the following answer I created a simple plugin to illustrate the problem with easy to read code. <?php # -*- coding: utf-8 -*- /* Plugin Name: Anonymous OOP Action */ if ( ! class_exists( 'Anonymous_Object' ) ) { /** * Add some ...


76

Simple to do, first create the dropdown with just the meta values you want and then catch the submit of the filter, just change POST_TYPE to the name of your post type and META_KEY to the name of your meta key: <?php /* Plugin Name: Admin Filter BY Custom Fields Plugin URI: http://en.bainternet.info Description: answer to http://wordpress.stackexchange....


59

As of WordPress 4.1 there is a filter: script_loader_tag. You can use it to find the correct script: add_filter( 'script_loader_tag', function ( $tag, $handle ) { if ( 'contact-form-7' !== $handle ) return $tag; return str_replace( ' src', ' defer="defer" src', $tag ); }, 10, 2 ); Old answer There is no dedicated filter available … at ...


41

Well, there is another way. And better, I guess. You should look at register_post_type parameters. You should probably set them like this: 'public' => false, // it's not public, it shouldn't have it's own permalink, and so on 'publicly_queryable' => true, // you should be able to query it 'show_ui' => true, // you should be able to edit it in ...


39

Just use the the_content filter, e.g.: <?php function theme_slug_filter_the_content( $content ) { $custom_content = 'YOUR CONTENT GOES HERE'; $custom_content .= $content; return $custom_content; } add_filter( 'the_content', 'theme_slug_filter_the_content' ); ?> Basically, you append the post content after your custom content, then return ...


35

Try to see the function with better names: apply_filters( $filter_name, // used for add_filter( $filter_name, 'callback' ); $value_to_change, // the only variable whose value you can change $context_1, // context $context_2 // more context ); So when that function is called as: // wp-login.php line 94 apply_filters( '...


33

Overview Basically the "Plugin API," which summons Filters and Hooks, consists out of the following functions: apply_filters() - execute do_action - execute apply_filters_ref_array() - execute do_action_ref_array() - execute add_filter() - add to stack add_action() - add to stack Basic Internals Overall there're a couple of globals (what else in ...


32

Filters and actions are both assigned to hooks. Functions assigned to hooks are stored in global $wp_filter variable. So all you have to do is to print_r it. print_r($GLOBALS['wp_filter']); PS. add_action function makes a add_filter call. And the latter does $wp_filter[$tag][$priority][$idx]. NOTE: you can directly add this code in functions.php, and you ...


31

The wp_insert_post_data filter can do that: add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 ); function filter_post_data( $data , $postarr ) { // Change post title $data['post_title'] .= '_suffix'; return $data; }


28

Same can be done through functions.php in your (child) theme: add_filter("woocommerce_checkout_fields", "order_fields"); function order_fields($fields) { $order = array( "billing_first_name", "billing_last_name", "billing_company", "billing_address_1", "billing_address_2", "billing_postcode", ...


28

Better solution if you want to disable this: use a plugin. Same code as from Christine's comments: https://wordpress.org/plugins/disable-emojis/ Same code that also fixes the smilies to be the older ones: https://wordpress.org/plugins/classic-smilies/ Source: Me, since I wrote that code in the first place. https://plugins.trac.wordpress.org/changeset/...


27

A prettier solution would be to disable the capability of creating a custom_post_type: Simply pass the the parameter 'create_posts' => false, in the capabilities array when calling register_post_type. $args = array( 'label' => __( 'Custom Post Type', 'text_domain' ), 'description' => __( 'Custom Post Type', '...


25

The API you offer in a plugin or a theme depends on the logic of that specific code. There is probably no guide that applies to all situations. I am a contributor for multiple plugins with APIs, and what I have learned so far is: Do not offer an API until you really know how people use your code. Release the first two or three versions without any API. ...


21

I had the same problem you have. I just did a... let's say... not very beautiful solution, but it works and so far it's the only solution I have. I added a little JavaScript line. It needs jQuery, but I'm sure you can figure it out without. This is my tiny JS: $('p:empty').remove(); This works for me!


20

Instead of using the code from the question in functions.php, replace it with this: /** * Prevent certain plugins from receiving automatic updates, and auto-update the rest. * * To auto-update certain plugins and exclude the rest, simply remove the "!" operator * from the function. * * Also, by using the 'auto_update_theme' or 'auto_update_core' ...


19

A) The Base in Core As you can see in the Codex Template Hierarchy explanation, single-{$post_type}.php is already supported. B) Extending the core Hierarchy Now there're gladly some filters and hooks inside /wp-includes/template-loader.php. do_action('template_redirect'); apply_filters( 'template_include', $template ) AND: a specific filter inside ...


18

it might not be the exact answer you're after but i think i just found a pretty good workaround. Iv'e taken the following code from the twenty-eleven theme CSS (who's neatly responsive imho): /* Images */ .entry-content img, .comment-content img, .widget img { max-width: 97.5%; /* Fluid images for posts, comments, and widgets */ } img[class*="align"], ...


18

Yes there is, you can use the woocommerce_get_price filter hook to filter the value based on user role and return a price accordingly e.g: add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2); /** * custom_price_WPA111772 * * filter the price based on category and user role * @param $price * @param $product * @return */ ...


18

Twenty Twelve does not have any hooks that fire immediately after the opening <body> tag. Therefore you in your child theme which extends the parent Twenty Twelve theme, copy the header.php across to your child theme directory. Open the header.php file in your child theme and just after the opening body tag add an action hook which you can then hook ...


18

The dialog HTML comes from WP_Editors::wp_link_dialog() but no hooks in there. We could instead use jQuery to append the custom HTML to the link dialog and try to override e.g. the wpLink.getAttrs(), because it's very short ;-) Demo example: jQuery( document ).ready( function( $ ) { $('#link-options').append( '<div> <...


18

Ah I just had this problem myself! And while _embed is great, in my experience it is very slow, and the point of JSON is to be fast :D I have the following code in a plugin (used for adding custom post types), but I imagine you could put it in your theme's function.php file. php add_action( 'rest_api_init', 'add_thumbnail_to_JSON' ); function ...


17

The best thing to do here is to use a static class. The following code should be instructional: class MyClass { function __construct() { add_action( 'wp_footer', array( $this, 'my_action' ) ); } function my_action() { print '<h1>' . __class__ . ' - ' . __function__ . '</h1>'; } } new MyClass(); class ...


17

Problem: The problem with your current snippet is that you are just checking the global main query object, no matter what the current query object is. Workaround: Note that the second input argument for the posts_where filter callback, is the current query object. Use that to determine if it's the main search query on the front-end with: add_filter( '...


16

The embed_oembed_html filter runs before an oEmbed resource's HTML is outputted, so you could hook into this and wrap the output in a div as below. I can't think of a simple way of wrapping the other content. add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4); function my_embed_oembed_html($html, $url, $attr, $post_id) { return '<div id="...


16

Here's the solution I used: function wrap_embed_with_div($html, $url, $attr) { return '<div class="video-container">' . $html . '</div>'; } add_filter('embed_oembed_html', 'wrap_embed_with_div', 10, 3);


16

QUESTION AND ANSWER REVISITED There are sometimes these questions that nags you and hunts you down later in life again, and this is one such question. This question had me thinking about an alternative solution to the problem. As I already stated, custom fields and meta boxes are there to store small pieces of meta data, and not to act as an extension to ...


16

There's some confusion here, because not all of these are validation, there are 2 others that are necessary to understand what's appropriate: validation sanitisation escaping Sanitisation Sanitisation makes things clean and well formed This cleans up the data, e.g. trimming trailing spaces, removing letters in a number field, making an all lowercase ...


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