Completely custom page in WordPress generated by a plugin - wordpress

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.

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

Wordpress: how to implement a sequence of pages other than template hierarchy

Being fairly new to Wordpress, I’m trying to understand how to implement a sequence of pages other than the sequence suggested by the template hierarchy.
Consider (as an example) a website where different products can be purchased. We have the custom post type “products”. Products can be selected and added to the cart. When the user wants to check out, he will be offered a sequence of pages for doing the checkout for one or more products.
What would be the best way to implement such a sequence? In normal websites, it is just a number of pages linking to each other. In Wordpress, at the time of writing the code the page id’s are unknown and will only be known after creating the pages in wp admin. After this, we can use the page id’s, but when rolling out to a different environment (for example from development to test or live), we have to create the pages again in wp admin, resulting in new page id’s. The page id’s will have to be altered in code. This is undesirable in my opinion, error prone, right?
Is there a way of dealing with this in Wordpress other than synchronizing databases? In other words, what is the best design for implementing a sequence of pages linking to each other, other than the normal wordpress template hierarchy flow?
As a solution I created a way to access permalinks based on logical id's both from php and javascript. When rolling out to a different environment only the mappings from logical id's to urls should be changed in one array in functions.php and you're done.
To enqueue your .js file in functions.php:
wp_register_script( 'aom_script', get_template_directory_uri() . '/script.js', array('jquery') );
wp_localize_script( 'aom_script', 'my_js_permalinks', aom_get_page_permalinks() );
wp_enqueue_script('aom_script');
Code in functions.php for storing and accessing the permalinks:
function aom_get_page_permalinks() {
$page_permalinks = array(
"home"=>get_bloginfo('url'),
"someurl"=>get_permalink(14),
"anotherurl"=>get_permalink(16)
);
return $page_permalinks;
}
function aom_get_page_permalink($page_id) {
$page_permalinks = aom_get_page_permalinks();
return $page_permalinks[$page_id];
}
Now if you need a permalink in php:
aom_get_page_permalink('someurl');
And if you need a permalink in javascript:
my_js_permalinks.someurl;

How can I successfully use browser-sync with wordpress locally?

I'm trying to get browser-sync to work with my multipress wordpress install, for simpler mobile / responsive development.
Currently I'm having problems in that, my normal development takes place at local.example.com, and browser-sync is proxying this (via 123.456.78.9:3202, as per browser sync).
So far browser-sync loads the site, but none of my scripts or CSS are loading (although images load fine). They jsut fail with no response in the network panel.
I'm using NGINX for hosting the site, as opposed to apache.
Does anyone have any wordpress browser-sync experience? Am I missing something with the browser-sync set up? And tips for this would be super welcome. I'd love to get this as a solid part of my work flow.
The problem is to do with how wordpress handles URLs, in that it normally uses full URLs for including content and links etc.
The proxy is trying to access these on another domain and that's why it's failing.
Update
A much simple, cleaner and maintainable strategy, that also helps with development environments is to use the Root Relative URLs plugin. Adds hooks and configs similar to below, but also updates your content and editors to apply the same structure, so it's a bit more robust
Original Answer
You can add a simple hook (source: wordpress relative urls) to filter wordpress generated urls and remove the base domain so you get relative links to styles and posts etc:
$relative_url_filters = array(
'script_loader_src', //js
'style_loader_src', //css
'post_link', // Normal post link
'post_type_link', // Custom post type link
'page_link', // Page link
'attachment_link', // Attachment link
'get_shortlink', // Shortlink
'post_type_archive_link', // Post type archive link
'get_pagenum_link', // Paginated link
'get_comments_pagenum_link', // Paginated comment link
'term_link', // Term link, including category, tag
'search_link', // Search link
'day_link', // Date archive link
'month_link',
'year_link'
);
foreach ( $relative_url_filters as $relative_url_filters ) {
add_filter( $relative_url_filters, 'wp_make_link_relative' );
}
Which should clean up most of your issues and get browser-sync working nicely.
I'm still having some issues where I have more complex inclusions for images, but more or less it's working and we're already seeing how cool it is!

Custom Plugin for wordpress with hierarchy of SEF pages

Here's my issue. My company needs a vendor database added to our wordpress website. None of the existing plugins will even come close to what we need, and we already have a mysql database with all of our information, so we need to create a plugin or something to do what we need.
These urls need to be direct-accessible and have SEF urls. So, for example:
mysite.com/vendors/
mysite.com/vendors/pipe-manufacturers/
mysite.com/vendor/bobs-pipes/
And, the custom content needs to appear inside the wordpress template.
There are really 2 options:
1) Find a way to write our application outside of wordpress, but find a way to bootstrap wordpress to show the header, footer, and sidebar.
2) Run the app from inside wordpress.
So I went for option #2. I created a new template file named "vendor.php", and began working. I added this code to my functions.php of my theme:
add_filter( 'template_include', 'xyz_template_check' );
function xyz_template_check() {
global $template;
$rqst = $_SERVER['REQUEST_URI'];
$ra = split("/", $rqst);
if ($ra[1] == "vendors") {
$template_file = get_stylesheet_directory() . '/vendors.php';
return $template_file;
}
return $template;
}
So what the above code does, if it sees the word "vendors" as the first part of the url after the site name, it sends you to vendor.php. This works PERFECTLY....
except...
Wordpress believes that the page is not found. It returns a 404 header, and NOT FOUND into the page title and breadcrumb.
Adding a PAGE called "Vendor Database" with the permalink "/vendors/" fixes the main page. But there will be literally hundreds of vendors and different categories. I cant be creating a custom page for each one. This needs to be dynamic.
So, how do I make wordpress give a 200, and supply an acceptable page title, breadcrumb, etc.
Don't even get me started on the danged wp_title filter. This did NOT work as documented. Although, it just occurred to me that this might be an issue with Wordpress SEO (the wp_title filter issue).
Anyone got an idea on this?
Ok got this. The solution was to use the rewrite api, as mentioned above, to look for the pattern /vendors/, letting it know that it was a valid URL. Coupled with my existing template override, this is what I needed.

Resources