Shortcodes — Using add_action, add_filter in the shortcode

A client wants the following:

  • User clicks on a specific button which takes them to a partner’s site.
  • If this is their first time clicking the link, they need to accept terms and conditions.
  • If they’ve already accepted, button takes them directly to link.
  • The client insists on being able to update everything (URL, button text) themselves, so I’m can’t hard code those values into the function.

In order to pull this off, I wrote a shortcode with attributes, and I’m using Gravity Forms hooks (gform_after_submission and gform_confirmation). I want these hooks to reference the shortcode attributes.

So if we use: [big_data_EULA url="http://www.example.com"] then gform_confirmation can use $ url.

— —

I’ve tried two, things:

  1. Including the add_action and add_filter within the shortcode function
  2. Passing the variables from the shortcode to those hooks.

Could anyone help me figure out the right way to get this working?

Here’s my code:

 function big_data_EULA_function($  atts){    extract(shortcode_atts(array(       'url' => 'http://www.google.com',       'confirm_text' => 'ACA Big Data Resource',       'click_thru_text' => 'ACA Big Data Resource',       'form' => '22',     ), $  atts));         /// Some of these variables aren't used in this iteration but I would like to use in final form.        $  EULA_url = $  url;        $  EULA_confirm_text = $  confirm_text;        $  EULA_click_thru_text = $  click_thru_text;        $  EULA_gravity_form = $  form;        $  EULA_user_ID = get_current_user_id();        $  EULA_meta_key = "_bigdata_EULA";        $  check_meta = get_user_meta( $  EULA_user_ID, $  EULA_meta_key, true );         if ($  check_meta) {           $  button ='<div class="box-button white-button optimize-button"><a href="' . $  EULA_url . '">' . $  click_thru_text . '</a></div>';          return $  button;         } else {           ob_start(); // start a buffer            echo '<div style="display: none; max-width: 60%;" id="hidden-content">';            echo gravity_form( 22, false, false, false, '', true );            echo '</div><div class="box-button white-button optimize-button"><a data-fancybox data-src="#hidden-content" href="javascript:;">' . $  EULA_confirm_text . '</a></div>';          $  output_string = ob_get_contents();          ob_end_clean();          return $  output_string;       };   };  // Update the user_meta flag only if they've actually accepted the EULA, ie, submitted the form. add_action( 'gform_after_submission_22', 'bigdata_EULA',10, 1 );   function bigdata_EULA() {     $  EULA_user_ID = get_current_user_id();     $  EULA_meta_key = "_bigdata_EULA";     $  current_date = date("F j, Y");      update_user_meta( $  EULA_user_ID, $  EULA_meta_key, $  current_date);   };   // Redirect to the partner site. The redirect URL should come from the shortcode. add_filter( 'gform_confirmation_22', 'custom_confirmation', 10,1 );   function custom_confirmation( $  confirmation) {     $  EULA_url = 'http://www.google.com';      $  confirmation = array( 'redirect' => $  EULA_url );   return $  confirmation; }   add_shortcode('big_data_EULA', 'big_data_EULA_function'); 

Update variable value via add_filter

I have this apply filter in my plugin.

$  pre_html = apply_filters( 'events_views_html', null, $  view_slug, $  query, $  context ); 

I want to change $ view_slug value dynamically because I do not want to modify plugin files. Can I do this using add_filter in my child theme ?

It is a basic question but I have limited knowledge of WordPress filters and hooks. Some guidelines regarding this will be appreciated.

add_filter to specific WooCommerce Category

I ‘m trying to show stock availibility for a WooCommerce category ‘workshops’ in this case. The code here works, but works on everything in the store. How can I apply this to just the ‘workshops’ category?

<?php add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2); function wcs_custom_get_availability( $  availability, $  _product ) {    global $  product;      // Change In Stock Text     if ( $  _product->is_in_stock() ) {         $  availability['availability'] = __('Available!', 'woocommerce');     }      // Change in Stock Text to only 1 or 2 left     if ( $  _product->is_in_stock() && $  product->get_stock_quantity() <= 4 ) {         $  availability['availability'] = sprintf( __('Only %s left!', 'woocommerce'), $  product->get_stock_quantity());     }      // Change Out of Stock Text     if ( ! $  _product->is_in_stock() ) {         $  availability['availability'] = __('Sorry, we sold out!', 'woocommerce');     }      return $  availability; } 

How to run add_action() or add_filter() after a button click?

I wrote a simple plugin to append some text to every post content by using add_filter(), which is working fine. but now i want to run the add_filter only after a button is clicked. so i but a button into my options page and added a function to execute after it is clicked. but the problem is when i put add_filter() inside that button action function, it won’t execute. Can anyone help me with the code? thanks!

This is working as it is in the root of php file.

add_filter("the_content", "appendAction");  function appendAction($  the_Post) {     $  the_Post.=" Bye ! ";     return $  the_Post; } 

but not working when put in a button action function

function test_button_action(){     add_filter("the_content", "appendAction");     function appendAction($  the_Post) {        $  the_Post.=" Bye ! ";        return $  the_Post;    } } 

Using add_filter inside another class

I’m building a plugin which generates custom css, I have 2 classes:

  1. The main class, with core functions
  2. The compiler class

as following (I simplify everything, because there is a lot of options):

class compiler  {    public function __construct(){       $  this->generateCss();       $  this->enqueueCss();       //other functions    }    public function generateCss(){       //this is the filter       $  css = apply_filters('dynamic_css', '.your_code{}');       $  content = $  css;       //the rest of the function which generates and upload the file    }    //other functions to enqueue the generated file }  $  compiler = new compiler();  class my_class {    public function __construct() {       //blablabla       $  this->pluginSetup();    }    public function pluginSetup(){       //other functions to save css in option "custom css"       add_action('admin_init', array($  this, 'set_css'));    }    public function set_css(){       add_filter( 'dynamic_css', array( $  this, 'load_custom_css' ));    }    public function load_custom_css(){       //css has been inserted in option "custom css" through omitted functions       $  css = get_option("custom_css");       return $  css;    } } $  my_class = new my_class(); 

The file is generated and enqueued correctly, but the filter dynamic_css doesn’t work and I only get the default value of .your_code{}.

The code in option("custom_css") exists and is valid on var_dump().

I want to keep separated these 2 classes and I cannot find a solution, I tried to change the order of initialisation without success.