How to implement a WordPress Action Hook - wordpress

I'm using the All-In-One Video Player plugin and want to alter its behaviour by listening to events that the player emits and taking actions based on them.
I contacted plugin's support team and got a very good response that I'm sure would mean something to someone who understands WordPress - I'm not one of those people.
The support team suggested using the action hook aiovg_player_footer. It looks like I have to implement that function, but I have no idea where to write that code. Is there a specific file that I need to create / update in order to get implement this function.
My function will need to alter the HTML that the plugin produces. Is this just a case of doing something like
echo '<script>console.log("helo");</script>' ?

You should add the following code in functions.php file located in the root of your theme directory:
function so61638829_aiovg_player_footer()
{
// Do something
}
add_action('aiovg_player_footer', 'so61638829_aiovg_player_footer');

Related

add_action() in wordpress uses php file name as first parameter. What does it mean?

Recently I am trying to understand the codebase of a custom wordpress plugin.
Some syntax of add_action() looks confusing to me.
add_action( 'after_plugin_row_wc-smart-cod/wc-smart-cod.php', array( $this, 'add_warning' ) );
I know the first parameter of add_action() is the name of the action to which the function 'add_warning' of $this object is hooked.
But in the above example the first parameter is the name of a php file, instead of an action name.
What does it mean ?
Thanks.
The first parameter to an action is a string. Generally it is something like wp_footer or before_send_email but there really isn't any restrictions on it. For instance, in my company's code, we often use slashes to give the action a (fake) namespace such as company/plugin-name/class/action but this is 100% arbitrary from WordPress's perspective.
Back to your example, however, there actually is a specific pattern from WordPress for that specific hook which you can see here. Every plugin in WordPress has an "entrance" or "plugin" file that boots up the entire plugin. Because most plugins live in a sub-folder, it is often plugin-name/plugin-name.php or plugin-name/index.php but it is ultimately up to the plugin author.
Most people use that specific action to add special messages to their own plugin's row in the general listing of plugins. The plugin you mentioned is using it to give a warning to users with a very specific version of the plugin installed.

Call wordpress plugin methods from functions file

I'm looking to utilize the woocommerce paypal pro plugin in some rest API calls, in my theme's functions.php file. The goal would be to get the constructed html form in one call, and process payment with another.
I still have not quite wrapped my head around how to interact with the plugins classes. Is there a simple explanation or an example of this sort of pattern (for any similar plugin setup) that can be used?
Now that I'm a bit more awake...
It's as simple as interacting with the plugin methods as normal PHP class:
$ppp = new WC_PP_PRO_Gateway();
$html = $ppp->payment_fields();

How to check AMP endpoint in WordPress?

I build a plugin that base on plugin https://wordpress.org/plugins/amp/ and I want to check state when the user on AMP post or page.
What is the function that allows us to check AMP endpoint in WordPress?
You can use the is_amp_endpoint() function to check if the currently loaded page/post is an AMP one.
I strongly suggest to use the is_amp_endpoint() function and wrap it within an existing condition, such as the following:
if (function_exists( 'is_amp_endpoint' ) && is_amp_endpoint()) {
// do stuff
}
That way your Wordpress web site won't crash if/when the WP-AMP plugin gets disabled (or changes its behaviour / method signature).
For additional info and alternative ways to do that, check out this blog post that I wrote on this topic.

Fire an action right after Appearance > Theme Options has been saved

I just started working with Wordpress (v. 3.6.1).
I have OptionTree installed and as it seems it handles the Theme Options page. I want to run my function (in a plugin or wherever else) right after the user saves the changes of this page.
So far I found out that option-tree/includes/ot-settings-api.php generates the form and it sets the form action to options.php (which is a wordpress core file). I was thinking about change the action to my custom php file and handle the save procedure and finally runs my own function. But this solution looks pretty ugly.
I wonder if there's another way to get the job done.
Thanks.
Thanks to #Sheikh Heera link (tutsplus) I could find a solution.
I think this is some kind of hack and I still don't know if it is the best way. Anyway I did this:
Create a file your-theme-settings.php in your theme lib folder.
Let Wordpress knows about your file by adding this code in your theme functions.php:
include_once('lib/your-theme-settings.php');
Add this code to your-theme-settings.php:
function your_theme_register_settings() {
register_setting('option_tree', 'option_tree', 'your_theme_validate_options');
}
function your_theme_validate_options($input) {
// do whatever you have to do with $input.
}
add_action('admin_init', 'your_theme_register_settings');
In step 3, I put 'option_tree' as 1st and 2nd argument of register_settings function, because I noticed that the Option Group and Option Name of OptionTree plugin is option_tree.
I'm not sure if this is the best solution, so I would be glad if you shares your ideas.

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Resources