WordPress hook after post content and meta update

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; } // Check post revision. if ( … Read more

How to override include_once pointed file using add_filter?

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 … Read more

How to render an element, that was saved as a template, using a hook?

You can create a template tag in your functions.php file like so: if ( ! function_exists( ‘mytheme_reusable’ ) ) : function mytheme_reusable() { //put your HTML or whatever code you need here } endif; You can then simply apply the template tag in any template exactly where you need it: <?php mytheme_reusable(); ?> Alternatively you … Read more

Adding JavaScript file in Admin Panel

Yes, your code is good. And that my_custom_script is a unique identifier for the script you’re enqueueing. You can find the default script handlers/identifiers here — e.g. jquery for the jQuery library that ships with WordPress. There you can also find more details about the wp_enqueue_script() function, e.g. where should you use the function, the … Read more

hooks for automatic approve user registration according to data in custom fields

## Try this if you are using Woocommerce function ws_new_user_approve_registration_message(){ $not_approved_message=”<p class=”registration”>Send in your registration application today!<br /> NOTE: Your account will be held for moderation and you will be unable to login until it is approved.</p>”; if( isset($_REQUEST[‘approved’]) ){ $approved = $_REQUEST[‘approved’]; if ($approved == ‘false’) echo ‘<p class=”registration successful”>Registration successful! You will be … Read more

How to use pre_get_posts

pre_get_posts happens just before a WP_Query needs to go to the database to grab stuff. Since every page has a main query, that can be changed. Does it go in the search template? No! By the time templates are loaded, the main query has already decided what to grab and gone to the database. Newcomers … Read more

Adding code before post title with the_title produces weird results

the_title filter has two parameters passed to it, $title and $id. You could use the $id to check the current post type and then do stuff based on that. add_action (‘the_title’ , ‘test’, 10, 2); function test($title, $id) { return ‘post’ === get_post_type($id) ? ‘<div>test</div> ‘ . $title : $title; } The problem with this … Read more

Can I “protect” a page with a form asking for an email address?

What about filtering the_content? function se365701_require_email ( $content ) { if ( is_page( ‘require_email’ ) ) && !isset( htmlspecialchars( $_COOKIE[‘submitted_email’]) ) { return $form; // your MailChimp Form Code } else{ return $content; } add_filter( ‘the_content’, ‘se365701_require_email’ ); Note that your form provide itself may have a cookie or you may need to set it … Read more

Redirect no product url’s to static url

If you prefer more control in coding, you may use request hook – test the WordPress query after it is being setup preg_match() – match /product/ keyword url_to_postid() – test if product url exists to build a checking when WordPress query is being setup. The advantage of code against .htaccess is that it is relatively … Read more