How to override default do_action output in Wordpress - wordpress

I'm trying to override the output in the footer of a Wordpress theme. The given section just has a do_action('action_name')
In my functions.php I've added:
add_action('action_name', 'action_name');
function action_name() {
echo "<p>Additional text</p>";
}
However, this outputs the content I wish to replace along with the content I want to add. I'm fairly new to themeing Wordpress so I'm fairly lost here. What am I doing wrong?

If you can find which function is doing the output you want to remove you can use remove_action() to remove it:
remove_action( $tag, $function_to_remove, $priority );
To find this you will have to look through the code of the theme or plugin that generates it. Somewhere will be a add_action() call similar to yours. Note the function name and priority and use remove_action() along with it in your plugin/theme.
The hard solution would be to remove all functions for that action with remove_all_actions().
Afterwards you can add your own output as you are doing now.
Note: Be mindful that using remove_all_actions() is not advisable on all actions. For example, the action wp_footer is used to load JavaScript at the end of the page. Removing all functions there will render plugins and themes that depend on it useless.

Check add_action related code here-
add_action ( string $tag, callable $function_to_add, int $priority =
10, int $accepted_args = 1 )
$tags (string) (Required) The name of the action to which the
$function_to_add is hooked.
$function_to_add (callable) (Required) The name of the function you
wish to be called
$priority (int) (Optional) Used to specify the order in which the
functions associated with a particular action are executed. Lower
numbers correspond with earlier execution, and functions with the same
priority are executed in the order in which they were added to the
action. Default value: 10
$accepted_args (int) (Optional) The number of arguments the function
accepts. Default value: 1
Have a look over below thread hope this will help you-
https://developer.wordpress.org/reference/functions/add_action/

Related

Using cpt and pass a variable using url

I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here

WP Shortcodes not being added

I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.

Wordpress - apply_filter that I can't connect to any add_filter

I am trying to edit a plugin for wordpress (woocommerce) and very often I find some lines that use the apply_filter function, for example one is this:
return apply_filters ( 'woocommerce_get_variation_sale_price', $price, $this, $min_or_max, $display );
Unfortunately I'm not able to understand what this filter do because I can't track back where the tag is used. I scanned (with search in eclipse) the workspace without any luck, I can't find any add_filter with this "woocommerc_get_variation_sale_price".
How is this possible? Reading the documentation of the two functions they should be connected...
I am stuck
The apply_filters() function will execute any function that has been been hooked to it using add_filter( $hook, $function_name, $priority, $num_arguments ) with the remaining values being passed to the function as arguments. This is generally how the WordPress Action and Filter hooks work to extend the functionality of core WordPress or plugins - WooCommerce in this case.
This means that the code in your example is optionally used to let you or other plugins change the value of $price that is being returned by the plugins. It is providing you with the parent object ($this) as well as other info about the variables used to calculate the price.
IF you can't find any other references to that string (your example is missing an 'e' in the filter name) then it means nothing is hooked to that filter and modifying the value of $price.
If you wanted to add a hook to filter the value of $price before it is returned it would look like the following.
// the name of the filter, the hooked function, the priority, and the # of args
add_filter( 'woocommerce_get_variation_sale_price', 'my_woocommerce_get_variation_sale_price', 10, 4 );
function my_woocommerce_get_variation_sale_price( $price, $product, $min_or_max, $display ){
// Use the arguments to do whatever you want to
// the $price before it is returned.
return $price;
}

How to extract the variables out from "add_shortcode" function in Wordpress?

In Wordpress (now in Template Fiile), lets say i have a custom Shortcode function, like:
In the PAGE:
[myshortcode id='1234']
Then, in the Template File:
function parseUserID_func( $atts ){
//parse the $atts here...
$userID = 1234; //now i get the userID is, 1234
}
add_shortcode( 'myshortcode', 'parseUserID_func' );
//now .. echo the value of $userID here? <----------
printout_UserDetails( $userID );
As you can see, how can i get the processed value of $userID variable out from the shortcode function please? (as in the last line)
The $userID is not touchable from the outside.
Actually what i'm trying to do is something like:
Pass an ID from the Page, using [myshortcode id="1234"]
Parse out the id from the shortcode's function. (parseUserID_func above)
When i get the id, i need to pass it to another custom function printout_UserDetails($id){} function (written in the Template File) .. to continue another processing works.
(Something like) then i will print out the USER DETAILS on the Page, from the printout_UserDetails() function.
This is why i need to pass a value from Shortcode, then parse it, then pass the id to my another custom function.
Any help please?
From what I'm understanding you're not trying to generate output with the shortcode, but to save data instead. I would try using custom fields to handle this.
If you need to output the "USER DETAILS" block within the post content (right where the shortcode is written), you should just merge the printout_userDetials logic into the actual shortcode handler function itself (in this case parseUserID_func).

Can I use a variable instead of a string for the Action Hook parameter of add_action() for Wordpress?

I need to construct several add_action() functions dynamically in a foreach loop which cycles through various custom taxonomies a user might have created.
The name of the action hook is 'category_add_form_fields', but the 'category' part of the hook changes for each name of custom taxonomy.
Example 1
So, if the plugin user creates a taxonomy called 'Colors', the hook would be 'colors_add_form_fields' and the add_action would look like:
add_action('colors_add_form_fields', 'myFunction',10,2);
Example 2
Or, if the plugin user creates a taxonomy called 'sizes', the hook would be 'sizes_add_form_fields' and the add_action would be:
add_action('sizes_add_form_fields', 'myFunction',10,2);
As it clearly can't be known in advance what every user is going to name their custom categories, I'm using a foreach loop which stores each category name as $customcat which I append to the string "_add_form_fields" and store the resulting string in $final for use in the add_action() function
The problem is that although I've tried various ways to insert this variable into add_action() it just doesn't seem to recognise them.
I searched Google but couldn't find anything, not even someone asking the same question.
Does anyone know what I should be doing? Or if variables simply can't be used, is there any other way I can achieve my objective using PHP?
These are some of the permutations I've tried so far...
$final = $customcat . "_add_form_fields";
$final = "'" . $customcat . "_add_form_fields'";
add_action( $final, 'my_function', 10, 2);
add_action( "$final", 'my_function', 10, 2);
add_action( strval($final), 'my_function', 10, 2);
There should be no problem using a $variable as the hooks name..
If this does not work for you - check if your hook is set , or if the arg. number is really 2, or the firing sequence or any other problem other than this particular one ..

Resources