How to check AMP endpoint in WordPress? - 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.

Related

How to implement a WordPress Action Hook

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');

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();

Hook to apply cache with _embed in Wordpress REST Api

I'm trying to cache the Wordpress REST Api response with the _embed parameter using the rest_pre_echo_response hook. This works good and I can cache the outgoing response. But I canĀ“t find a good hook to apply the cache. Using the rest_pre_dispatch hook I get the problem that the embedded links are still replaced since the cached response have embedded links. Is their a hook prior dispatch that skips the embedding of links? Or is their a better solution? Thanks!
In your filter 'rest_pre_dispatch' add
if ( $in_cache ) {
$_GET['_embed'] = '0';
}

Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it - blocks of HTML are fetched by the front end from the plugin using AJAX.
I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?
The content of the page will be HTML - the plugin can generate this in response to POST or GET parameters.
There needs to be a route to this page. The route does not have to be user-friendly or a REST style - just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
Ideally I would avoid any URLs that go into the wp-admin directory. End users don't belong in here.
I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.
wp_enqueue_script( 'ajax-script',
plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as
// ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you've described it.
Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.
To have access to the wordpress api as well though, you have two options:
Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
Set up a custom page or slug to direct to your plugin.
I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin
From Jason's comment, based on the link above:
The rewrite rules are mentioned a lot, but are really a distraction -
they just help to make URLs look more "friendly", which was not an
objective here. The key is: register a custom GET parameter; look for
that parameter early in the page rendering process; if you find the
parameter is set, then output/echo stuff and die(). There are a
number of hooks that can be used to look at the parameters, chosen
dependin on how much you want WP to set up and process first.

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