Access category within rss2_head hook?

Can I access the current category of a category feed with the rss2_head hook to add for example itunes tags?

Lets say I have wordpress.com/catx/feed I want to get acf field elements associated with this specific category.

Here is what I am trying to accomplish:

function itunes_head() {         $  category = get_the_category();         $  categories = get_category();         global $  post;         var_dump($  categories);         echo print_r($  post);         echo $  categories;         echo $  category; } add_filter( 'rss2_head', 'itunes_head' ); 

I am assuming that I somehow can retrieve the catx category here?

Unable to hit hook with form submit build with “wp_ajax_{$action}”

I’m all I’m currently trying to build a hook using do_action( "wp_ajax_{$ action}" ) on a Form submit. I built the following form.

 <div class="layout-gutter bg-grey-lightest p-4">       <div class="bg-grey-light p-4">         <form action="http://localhost:8080/wp-admin/admin-ajax.php" method="post">           <input name="action" type="hidden" value="feedback">           <?php wp_nonce_field( 'feedback_nonce' ); ?>            <fieldset class="c-question">             <legend class="c-question__label">Was this page helpful?</legend>             <div class="c-question__container">               <div>                 <label class="toggle">                   <input data-type="boolean" name="helpful" type="radio" value="1" />                   <span class="toggle__label">Yes</span>                 </label>                 <label class="toggle">                   <input checked="true" data-type="boolean" name="helpful" type="radio" value="0" />                   <span class="toggle__label">No</span>                 </label>               </div>             </div>           </fieldset>             <div class="c-question">             <label class="c-question__label" for="client-0-email-address">Why or why not?</label>             <div class="c-question__container">               <div>                 <input required="true" type="text" name="description" />               </div>             </div>           </div>            <div>             <button class="btn text-inherit" type="cancel">Cancel</button>             <button class="btn-primary" type="submit">Submit</button>           </div>         </form>       </div>     </div> 

But when I submit the form I am unable to hit the action hook. I have the following file lib/feedback.php

<?php namespace FeedbackNYC;  add_action('wp_ajax_feedback', 'FeedbackNYC\feedbackHandler'); add_action('wp_ajax_nopriv_feedback', 'FeedbackNYC\feedbackHandler');  function feedbackHandler() {   echo "Hellow World";   echo $  _POST:   // Do some code here } 

But when I submit the form I’m not getting to feedbackHandler. The form has a hidden input field <input name="action" type="hidden" value="feedback"> but I never get to the hook. I tried echo in strings but nothing gets returned back. The request returns a 404 error as shown below:

enter image description here

How can I hit feedbackHandler on form submission?

Woocommerce get_term_by() in transition_post_status hook doesn’t works

Im having this code

$  concours_termine_cat = get_term_by('slug' , 'concours-termine' , 'product_cat'); 

get_term_by is returning false, after debugging the get_term_by function

taxonomy_exists( 'product_cat');  

is returning false.

So I checked in taxonomy_exists function and looks like into $ wp_taxonomies product_cat doesn’t exist at this time of execution.

I call this hook like this add_action('transition_post_status' , 'vince_function_post_update' , 10 , 3);

Add WordPress hook outside of Plugin or Theme

We have the following code that is used to hide our core theme so that it can’t be enabled directly and you can only enable the child themes from the WordPress Dashboard.

function hide_core_theme($  themes) {     unset($  themes['nacore']);     return $  themes; } add_action('wp_prepare_themes_for_js', 'hide_core_theme'); 

However if you put this code inside the functions.php file of either the Parent or Child theme it will only hide the core theme when one of them is enabled… e.g. if someone was to turn on another theme the core theme would appear again.

Another option is to add this code inside a custom plugin… but it means having it enabled and a user could always disable this plugin to get around it… plus we’re not too keen on having a plugin to turn off a theme as it feels pretty detached.

Is it possible to add some hooks that are outside of a Theme or Plugin? And therefore are called regardless of what themes or plugins are enabled?

do_action hook is not loading the template parts from child-theme

I have the same exact file/folder structure in my child and parent them but there is this template-functions.php that is still being loaded from the parent theme.

The hook that is calling the function looks like this, do_action('underboot_branding_menu'); and underboot_branding_menu is a function in template-functions.php and this file is located in a folder called "inc". I know includes don’t search the child-theme but it doesn’t look this file is called using include().

I have tracked all the template parts and pieces and they are all being loaded from the child-theme expect for template-functions.php.

This is how the code calling the function looks like if it helps:

<?php             /**              *               * @hooked underboot_site_branding              * @see template-functions file              *               */             do_action('underboot_branding_menu'); ?> 

Add product to cart with hook, discount applies after refresh of page

I have this simple setup where I add a product to the cart as a gift if conditions are met like so:

add_action( 'woocommerce_before_cart', 'apply_membership_benefits' ); function apply_membership_benefits() {   global $  woocommerce;              // Get the cart subtotal in non-decimal number format   $  cart_subtotal = WC()->cart->subtotal;   $  product_id1 = 9617; // membership product       $  product_id2 = 9616; //  custom bottle product          // If cart subtotal is less than 800 remove benefits   if ($  cart_subtotal < 800) {       WC()->cart->remove_cart_item( WC()->cart->generate_cart_id( $  product_id1 ) );       WC()->cart->remove_cart_item( WC()->cart->generate_cart_id( $  product_id2 ) );   }       // If cart above 800 add membership benefits   if ($  cart_subtotal > 799 ) {            $  item_key_inc_cart = WC()->cart->add_to_cart( $  product_id1 );     WC()->cart->set_quantity( $  item_key_inc_cart,1 );            $  item_key_inc_cart = WC()->cart->add_to_cart( $  product_id2 );     WC()->cart->set_quantity( $  item_key_inc_cart,1 );          }  } 

The cart will work correctly only I also have a discount plugin that also applies a discount to this cart of 20%. The problem is the discount is applied if the user is refreshing the page so the cart content seems to be read before the woocommerce_before_cart hook.

Consequently if I remove one product from the cart adn the subtotal will be below 800 as shown in the example the gifts will be removed but the discount is still applied and needs a page refresh to be recalculated and eventually removed.

The discount plugin seems to add all the necessary data with these hooks

    // Prepare cart discounts     add_action('woocommerce_before_calculate_totals', array($  this, 'prepare_cart_discounts'), 1);      // Apply cart discounts     add_action('woocommerce_after_calculate_totals', array($  this, 'apply'));      // Remove no longer applicable cart discounts     add_action('woocommerce_before_calculate_totals', array($  this, 'remove_cart_discounts'), 2);     add_action('woocommerce_check_cart_items', array($  this, 'remove_cart_discounts'), 1);      // Invalidate no longer applicable cart discounts     add_filter('woocommerce_coupon_is_valid', array($  this, 'maybe_invalidate_cart_discount'), 10, 2);      // Register custom coupon type     add_filter('woocommerce_coupon_discount_types', array($  this, 'register_custom_coupon_type')); 

wp_schedule_single_event issue with save_post hook in gutenberg

I am using the "save_post" hook to call wp_schedule_single_event. Everything works as expected with the classic editor, however when I try a post in gutenberg there isn’t actually a cron entry that is getting created. I know it’s still calling the hook since it’s updating some post meta values. Any ideas why the cron entry is not saving with the wp_schedule_single_event call?

How do you run code on a specific page without missing the init hook?

The earliest that functions such as get_permalink or is_page are available is parse_query (afaik). Then I thought, ok, clearly, I can’t say if( is_page('myPage') ) { add_action( 'init'... // because is_page will always resolve to false.

So then I thought, well, how about wrapping parse_query inside init like so:

add_action( 'init', function() {     add_action( 'parse_query', function() use() {         if( is_page( 'myPage' ) ) { //     }, 10 ); }, 10 ); 

Well, that still does nothing. The code works, but hooking into init there doesn’t really work. My goal here is to make use of items that need to launch/are available on init:10. So, I said, ok, just pass them, right?

add_action( 'init', function() {   $  something_i_need_from_init_10 = //do something.   add_action( 'parse_query', function() use( $  something_i_need_from_init_10 )   // } 

Which works, now, there are two cases that you’ll use this for:

  1. Getting something. If it’s available on init:10, isn’t it available on parse_query since parse_query come after? Sure, for most things, but remember that WP core itself gets rid of certain get_post_* functionality once it hits certain actions. Perhaps you have a service container that works well with WP and a service is only available on init:10.
  2. Doing something. Perhaps something needs to fire here. If so, this still works.

But all of this is clunky to write.

Is there no way to tell WP to run code on init, on a certain page only? Just looking at the link won’t do. There are rewrite rules, link formats and so on that one has to take care of and that probably don’t load until later on.

Ultimately, it’d be nice if all I had to write was:

add_action( 'init', function() {   if( is_page( 'myPage' ) ) {     //do stuff   } } 

What can one do when trying to design this way?


I don’t know how to figure it myself, but perhaps pipelining this process through the page.php / single.php pages will work. I assume WP has to figure out very early on what page, with what template it has to serve for any given link, for which it calls these 2 (usually).