I wrote an "autocomplete" field in archive.php - typing in it calls a function defined in functions.php. Here is a sample of the code:
function ajax_search_archive() {
$term = get_queried_object();
if (!$term) return;
(... continued...)
}
I tried that in a "tag" archive page, then in a "category" archive page. Each time, the function was called, then returned 0 at the second line because the retrieved "get_queried_object" was null - though I expected it wasn't.
Please, what did I wrong?
Thanks again for the comments, which showed me the way to solve my own problem.
The function get_queried_object() seems to work when called in archive.php itself, but not when called from a function defined in another file - even if that other function is called in archive.php. That realization led me to do some updates:
a) in archive.php: retrieving the get_queried_object(), storing its id into a hidden field (e.g. "archive_id)";
b) the Ajax "autocomplete" call in its script: sending the object id as another argument (value retrieved using jQuery - e.g. "$('#archive_id').val()");
c) the function ajax_search_archive in functions.php: retrieving the object id from the POST request.
Related
I need to delete a transient for all shortcodes located on a post/page when that page is updated. So the page update should delete the transient just for the shortcodes located on that page. If that isn't possible it would help if all shortcode transients are deleted on any page update.
I tried using the save_post action but nothing happens, so I must be doing something wrong.
Also, the problem is that I'm generating a shortcode ID, based on post ID and incrementation of a static variable.
So, only when I call the method to delete a transient in shortcode-template.php I have access to the current page's ID and then I can delete a transient related to page ID/Shortcode ID combination. But then the transient is deleted every time the shortcode is loaded. And if I don't call the delete_transient function, it seems like it is not hooked to save_post action, because nothing happens. I guess that is because the current page ID can't be fetched in a Class but only when used somewhere on the page. But I have no idea how to go around that.
Here is my Class:
class MainClass {
private static $shortcode_increment = 0;
private function __construct() {
add_action( 'save_post', array( $this, 'update_page_delete_transient') );
}
public static function increment_id() {
global $post;
self::$shortcode_increment++;
return 'shortcode_' . $post->ID . "_" . self::$shortcode_increment;
}
// delete the shortcode cache on page update
public function update_page_delete_transient() {
delete_transient( 'weather_'.self::increment_id() );
}
}
Any help is appreciated. Thanks.
UPDATE: I'm not sure how actions should work, as I understand it, the funcion added to the action should be activated when that action occurs, so there is no need to call that function? Or is there? Will the function be added to the action be activated automatically after the Class is instantiated in shortcode-template.php for example, since the action itself is in the construct?
Or do I need to call the delete_transient function in shortcode-template.php in order for it to work? But when I do that, it just runs every time widget is loaded, and not on save post/page.
UPDATE 2: So, if I remove the post ID from the increment ID function, I can badly resolve the issue from update 1 above. Delete_transient function is activated when I create an instance of an object and it is called on page/post save. Now the problem that remains is how do I pass the unique Shortcode ID to delete_transient function, if the ID is increased only in the shortcode-template.php when a new shortcode is created. Or it would be even better if I could somehow pass the Page ID and Shortcode ID somehow to the delete_transient function in the Main Class, so I could delete a specific transient for a specific shortcode on update. Is there a way to pass the data from the shortcode-template.php back to the Object method?
I have a theme that has a plugin called Option Tree integrated in it to create a theme options panel (that plugin allows for a "theme mode" so it isn't installed as a plugin).
The Option Tree plugin allows you to access the saved data in the theme options by using this function:
$data_of_a_single_option = ot_get_option( 'name_of_option_field_to_retrieve', NULL );
I have another plugin that needs to get data saved in the theme option, but the theme (and option tree) is loaded after the plugin is, so calling the function leads to a "function does not exist" error.
Is there a way the plugin can call that function and get the data and be able to store that data in a variable?
I tried using an action like this:
function get_special_data() {
$test = ot_get_option( 'test_field', NULL );
return $test;
var_dump( $test );
}
add_action('after_theme_setup', 'get_special_data', 2);
// then just below in same plugin
$data_from_theme = get_special_data();
Because I read that Option tree is loaded after_Theme_setup with a priority of 1.
By using add_action, I can see the data (var_dump outputs it correctly), but I can't get the data from inside the plugin by calling the get_special_data() function because it sends a "ot_get_option function does not exist" error.
Is there any way to do this? Or am I trying to solve the wrong problem?
Thanks in advance!
I believe you are looking for the plugins_loaded action. That should work as long as the plugin defines that function without some other action required first to load the function.
Also, in your code example $data_from_theme = get_special_data(); is going to execute before the action callback you're defining actually fires.
I am looking for a function that will fire a php file when a category is updated. I don't really want to use a plugin, just want a function to go in functions.php. Can anyone point me in the right direction?
Thanks in advance.
Seems there was a function called "edited_category" but this seems to have been renamed to edited_{$taxonomy} .But I can find no documentation on how to use this. Anyone got any Ideas?
You can use edit_category hook
Runs when a category is updated/edited, including when a post or
blogroll link is added/deleted or its categories are updated (which
causes the count for the category to update). Action function
arguments: category ID.
You can add the hook in your functions.php file as given below
add_action ( 'edited_category', 'update_category_function');
function update_category_function( $category_id )
{
// ...
}
well im learning to create a wordpress plugin
i downloaded one and read the codes, and i saw this
i assume 'foo' is the tag where it will add action to..
but what does the array() exactly do?
add_action('foo', array('foo1', 'foo2'));
i looked at http://codex.wordpress.org/Function_Reference/add_action
and there is no clear definition about it ..
Right, the first argument is the tag (to which you'll be adding the action), and the second argument specifies the function to call (i.e. your callback).
The second argument takes in a PHP callback, and as such, accepts a number of valid forms. Check this out for all of them :
PHP Callback Pseudo-Types
The type you've shown above is of type 2. The first element of the array specifies a class, and the second element specifies which function of the class you'd like to call.
So with the example you've given above, what that will do is that, whenever the foo() action is called, it will eventually call foo1->foo2() as well.
The second argument of the add_action function is the function to be called with the hook.
function hello_header() {
echo "I'm in the header!";
}
add_action('wp_head', 'hello_header');
The usage of an array as the second argument is to pass a objects method rather than just a regular function.
Have a read up on the how the call_user_func works. Should provide some more insight.
http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback(dead link)
I'm trying to create a hook in one Wordpress plugin that could be used by other plugins. First off, is this even possible? I'm also sending some additional args so this may be 2 questions in one since I've been having trouble finding definitive information on how to do this.
Here is what I've tried so far:
In the plugin that is creating the hook (call it Plugin 1) I added:
do_action('plugin1_hook', $customArg1, $customArg2, $customArg3);
at the point that I want the hook to fire. Then, in a different plugin (Plugin 2), I added:
add_action('plugin1_hook', 'my_function');
and
function my_function($customArg1, $customArg2, $customArg3) { //my code }
This does not seem to be firing the function, however.
My refence for this has been the Wordpress hook comment_post, which is defined by Wordpress as:
do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
and I am using as:
add_action('comment_post', 'my_comment');
function my_comment($comment_id) { //my code }
The above snippet is functioning properly.
I thought I'd post this as an answer as it's a little clearer to explain :)
When you hook a function, but do not specify the number of arguments, WordPress will always pass back one argument.
You won't get errors for something like this;
function do_my_hook($arg1, $arg2 = '', $arg3 = '') {}
add_action('my_hook', 'do_my_hook');
But you will for something like this;
function do_my_hook($arg1, $arg2, $arg3) {}
add_action('my_hook', 'do_my_hook');
WordPress is trying to call do_my_hook(), but it's only passing back one argument. The first example uses PHP default function arguments, so that you can call a function without passing all available arguments, but without error.
The second example will trigger a 'missing argument(s)' PHP error, as all three arguments are required.
The fix?
add_action('my_hook', 'do_my_hook', 10, 3);
The idea behind defining how many arguments your function takes is to avoid errors like these (though technically they are as easily avoided using default arguments!).
My guess is the second plugin is loading after the first one, so the hook has already fired by the time you add an action to it. You might try this for the first plugin:
function my_custom_hook_insertion($arg1, $arg2, $arg3){
do_action('plugin1_hook', $arg1, $arg2, $arg3);
}
add_action('plugins_loaded', 'my_custom_hook_insertion');
That will wait until all plugins are loaded before firing the hook.
Changing my add_action to this fixed the problem:
add_action('plugin1_hook', 'my_function', 10, 3);
The 10 represents the priority, and the 3 represents the number of args that the function will take. I'm not exactly sure how the matching works, since the default is 1, and I use plenty of hooks without specifying 0 args and I've used hooks that pass more than 1 arg but only used 1 arg in my function signature. Source: WordPress Codex: Function Reference/add action
It is working though, so cross plugin hooks are possible.