Hot answers tagged

4

The 'body' needs to be an array, not including the 'json_encode($user)' piece. $response = wp_remote_post( 'myapp.com/endpoint', array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), 'body' => $user ) ); I have this in my function since I also had issues with the body being an object: if (...


3

It’s a problem as old as WP, or even older... You can’t set cookies, or send any other headers, if any of the content of the site is already sent. You try to set cookies using init hook. And it is OK in most cases, since no output should be printed yet. But... Not all code is written correctly. There are many other thing done before init. Take a look at ...


3

I suggest to take a look at the tests file to see different ways of using hooks. They work similarly to PHP hooks, you can set several addAction and then one doAction with the name of the hook and other arguments. The first argument of doAction has to be the action name. Then you can pass as many arguments as you want and those will pass to the addAction ...


3

Using the filter blocks.registerBlockType we can alter block registration settings. We simply need to target the core/image block and modify the default value for the attribute linkDestination which is none by default. function modifyLinkDestinationDefault(settings, name) { if (name !== "core/image") { return settings; } settings....


3

You can use pre_get_posts action to modify this order. Here's the code: function my_change_posts_order( $query ){ if ( ! is_admin() && ( is_category() || is_tag() ) && $query->is_main_query() ) { $query->set( 'order', 'ASC' ); } }; add_action( 'pre_get_posts', 'my_change_posts_order'); What we do here is: We're ...


3

You can change that in wp-config (https://codex.wordpress.org/Editing_wp-config.php#Empty_Trash): define( 'EMPTY_TRASH_DAYS', 1 ); // Integer is the amount of days


3

I think there are two decent options here. Let WordPress keep its mail function, but hook in once or twice wp_mail() has several hooks that will be helpful for you to replace the built in behavior, which is basically to use php's simple little mailer. Here are a couple of those helpful hooks that I think you might use, since that's what you asked for: /...


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

I think you're on the right track with a filter. Have you tried login_body_class as the filter? function add_blog_id_to_login_page( $classes ) { $blog_id = get_current_blog_id(); $classes[] = "blog-{$blog_id}"; return $classes; } add_filter( 'login_body_class', 'add_blog_id_to_login_page' );


3

The parse_query() method of the WP_Query class sets the variables on which the conditional tags are based. The parse_query hook are executed before returning from parse_query() method, variables are already set (eg. is_home, used by is_front_page()) and is_front_page() will work, but function that changes the current state can be hooked to parse_query (it's ...


3

create_category runs when a new category is created, in turn set_object_terms fires after a terms have been set to object. The hooks you are looking for are: create_{$taxonomy} - new term in a specific taxonomy is created created_{$taxonomy} - new term created, term cache has been cleaned create_term created_term


3

How to use wp.hooks.addAction? It's basically like so, just like you've attempted: // Hook to the hook_name action. wp.hooks.addAction( 'hook_name', 'namespace', function(){ console.log( 'Foo Bar' ); } ); // Trigger the hook_name action. wp.hooks.doAction( 'hook_name' ); Or for hooks which provides one or more parameters to the callback: // Hook to ...


2

Edit your single.php or single-post.php template, depending on which one is present and in use with your theme. The JS file should be registered and conditionally enqueued when the template is loaded. This allows for dependency management and lots of other easy management actions. Another way to consider is make your menu an actual WordPress menu or 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

All you need to do is to modify your method of restricting users. add_action( 'admin_init', function() { if ( (defined('DOING_AJAX') && DOING_AJAX) || ( strpos($_SERVER['SCRIPT_NAME'], 'admin-post.php') ) ) { return; } if ( !current_user_can('manage_options') ) { wp_redirect( home_url('/meu-perfil') ); exit(); ...


2

In order to achieve a deeper "injection" into the search and catch what the user has searched as soon as possible, we have to look at the WP_Query class and see how it treats a query for posts: In its parse_query function wee that it uses a flag to signal to the whole class that, indeed, the query is a "search", not surprisingly, it looks for s inside the ...


2

Yes, you can. But you have to be careful, since some special cases may occur. Some of them are: 1. Function may be class method and not a global function: class SomeClass { ... function some_function() { ... } ... } In such case, you can use it only if: - it's a static function - it's public method and you have access to some object of that ...


2

You can use functions defined in the parent theme, but you can only use them after the parent theme is loaded. The parent theme is loaded after your child theme. This means that if you want to use a function from the parent theme you can only do it inside a function that is hooked into a hook on after_setup_theme or later. So your example will only work if ...


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

To override action that use in plugin, use can wrap the remove_action code in template_redirect action to make sure your action will be excute later. For example add_action('template_redirect', function() { // Remove the title. remove_action('sensei_course_content_inside_before', array( 'Sensei_Templates', 'the_title' ) ,5, 1 ); // Add it ...


2

To do this you would indeed have to include some code in the functions.php of your theme. You can use admin_notices to add a note to this specific page like this: add_action( 'admin_notices', 'wpse332074_donotedit' ); function wpse332074_donotedit () { $screen = get_current_screen(); if ($screen->post_type == 'page') { $variable = $_GET['post']; ...


2

If we take your code at face value, there are some consequences: it runs on every request, be that a page, an AJAX call, a REST API request, even XMLRPC It's a super expensive post query that involves post meta, with no upper limit on the number of results You are filtering a post query by a posts meta values, but that's not what post meta is for. That's ...


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

Note that the third input argument for the save_post callback, tells us whether it's an update or not. That could be helpful here, I think. Taking your example as is, one suggestion could be: add_action( 'save_post', function( $post_ID, $post, $update ) { postUpdate( $post_ID ); if ( $update ) { deleteCacheOnCloudfront( $post_ID, $post, $...


2

Activation hook is run only after the plugin get activated. So only once and only during that one request. It's meant to allow author of a plugin to perform some actions after the plugin is activated. Actions, that need to be done only once. For example if your plugin uses custom database table - you can use activation hook to create such table. On the ...


2

You can use wp_authenticate filter for that... This filter is fired before the user is authenticated. Params are passed by reference, so you can modify them. // user name is passed in by reference function wp_authenticate_by_email( &$username ) { // make sure that the condition below works for you if ( is_numeric( $username ) && '0' == ...


2

add_action's second parameter is a callable, it can accept a string (like what you did in the example) or an array of class-instance and function name. For example, if you want to call a method get_age() from Person class, you can do this: $person = new Person(); add_action( 'hook_name', array($person , 'get_age') );


2

During page request, Wordpress actions are triggered in a sequence, therefore you have to register them in time. In your case, wp_head comes after wp_enqueue_scripts, so as a solution, i would all remove them from the wp_head and register them all in a condition, outside of any other action: if ( isset( $_COOKIE['user_opt'] ) ) { add_action('...


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