How to find out which page is in use - wordpress

iThemes exchange has this function that I can call from anywhere in my theme which tells me if one of the iThemes pages is active. So for example if I wanna find out if the current page is an iThemes exchange page I do:
if (it_exchange_is_page()) {
or if I wanna find out if its the login page I do:
if (it_exchange_is_page('login')) {
I'm trying to figure out how to make a function like this for my own membership plugin. So I tried adding this function to the bottom of my membership plugin file:
function check_members_page() {
if ( has_action('custom_members_page') ) { return TRUE; }
else { return FALSE; }
}
then in the function which renders the login page, I add:
add_action('custom_members_page','fake_function');
the problem is that the render login page function gets called after I run has_action so fake_function hasn't been hooked to the custom_members_page hook yet. What do you do in situations like this where you need to make a function that will be available everywhere (i.e. in your theme, in other plugins etc.), but that function needs some information from somewhere further down?

The problem was that I was trying to call these wordpress functions directly from the functions.php file, before many of these functions (like get_post_meta or the $post object) are even defined. I'm still getting my head around the whole action hooks thing.

Related

How to POST to admin-post.php from Avada formbuilder form

Using the Avada theme's form builder functionality, I’m attempting to use the “Send to URL” option to POST the form and then run an api call in a plugin I’ve written.
I was thinking I could set the “Send to URL” value to /wp-admin/admin-post.php and add a hidden field to the form named “action” with a value of “make_api_call” to get it to run the code in my plugin set up like so:
add_action('admin_post_make_api_call', 'make_api_call');
function make_api_call()
{
//todo: make the api call
wp_redirect(‘another-page’);
}
However, this does not work and returns this from the server: {"status":"error","info":"url_failed"}
What I want to do is to POST the form to admin-post.php, have my plugin run code when it POSTs, and then redirect to another page.
I've checked the documentation for the AVADA theme and it only says that you can specify a url to post to but doesn't give any additional details.
So I eventually got something to work, and I'm not knowledgeable or experienced enough with WordPress development to know if it is the "right" way, but it works well.
I realized that using the "Send to Url" option on the Avada form, it was POSTing the form to the admin-ajax.php file. There's plenty of documentation on that and I was able to partially make that work but I was not able to make it fit my use case b/c even though there is a way to configure the Avada form to redirect to a different URL on success I couldn't append parameters to that URL based on the return value from admin-ajax.php.
For future reference, here's what I was able to make work but not fit my use case by having the Avada form submission set to Send to Url. (I'm recreating this and some of it's from memory since I went with a different solution, so it may not be 100% runnable.)
The way admin-ajax works is all requests to admin-ajax.php are eventually handled by a WordPress action (filter?) like so:
add_action( 'wp_ajax_my_action', 'my_function' );
In the above, my_action is what you've set as the form's action by creating a hidden input element on your html form named action and setting it's value to "my_action". The my_function argument is the name of the function you want to run when the action happens.
add_action( 'wp_ajax_my_action', 'my_function' );
function my_function(){
//do stuff
}
Watching the request in Chrome's dev tools, I could see the action the form was setting was fusion_form_submit_form_to_url.
So ended up with this:
add_action( 'wp_ajax_fusion_form_submit_form_to_url', 'my_function' );
function my_function(){
//do stuff
}
You can see that the url you enter in the Form Submission URL field gets passed to admin-ajax as fusionAction. Whether the Avada theme does something additional with that - I don't know but you could use it to control the logic that gets executed in my_function. I suspect there's an action in the Avada form that works similar to the wp_ajax_ WordPress action but by the time I got this far I realized this wasn't going to work so I pivoted to the actual solution, below.
All of that worked okay but you can't redirect out of a call to admin-ajax.php unless you do it on the client side and I didn't want to dive into that.
What I was able to make work was configuring the Avada form to do a traditional HTTP POST. I added a hidden input element on the Avada form with a name of formName and the value set to the name of the form I wanted to handle.
In my plugin code, I hooked into the WordPress init action as in the code sample below, and then customized the logic to be executed based on which formName was sent in.
add_action('init', 'callback_function');
function callback_function()
{
if (isset($_POST['formName'])) {
$form = $_POST['formName']; //from the hidden input element "formName"
//there is a call to wp_redirect in each case
switch ($form) {
case 'form1':
process_form_1();
break;
case 'form2':
process_form_1();
break;
default:
# code...
break;
}
//without this "exit" you will get errors similar to "headers already sent"
exit;
}
}
This allowed me to run the code I needed to run based on what form was submitted, and redirect to the correct place afterward.

How to call WordPress plugin function from custom page template?

I have a custom WordPress plugin that handles authentication.
There's a function logTheUserIn() inside plugin-name/src/Classes/Auth.php.
I need this function to be run when a user hits a custom WordPress template page (page-authPortal.php), which has this code at the top:
include_once('wp-includes/pluggable.php');
include_once("wp-content/plugins/ad-auth-bridge/src/Classes/Auth.php");
print "test";
I created a WordPress page titled "authPortal" and it shows the 'test' text, so I know the custom page is being loaded and rendered. Now I just need to fire off logTheUserIn().
I have tried adding shortcodes and actions inside Auth.php:
class Auth {
public function InitHooks() {
add_shortcode ('authNow', 'logTheUserIn');
add_action ('authAction', 'logTheUserIn');
I've then tried to use the actual shortcode [authNow] inside the WordPress editor, I have also tried do_shortcode and do_action.
What am I missing here?
Thank you!
There is no need to include or require the plugin fields. They are initially loaded by WordPress.
First, in your template, make sure your plugin is active and the function/class is reachable:
if ( function_exists( 'logTheUserIn') ) {
logTheUserIn();
}
Then fire the function right within your template.
In your case, you might need to check if class_exists, then initialize the class

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

how to hide a page from being seen in wordpress backend and frontend

In my plugin i have created a custom template that prints a requested sidebar. and for running the code of this template i assigned a custom page to it (by calling update_metadata) .
Is it a good idea for getting content of a specific sidebar into Ajax call ?
Now my problem is that WORDPRESS shows it in the dashboard and front page , and after searching i have not found any easy to understand solution for Hiding a page completely so only can be accessed by its id .
Can any one tell me how to do that ?
you are going about this the wrong way. You can create a function that can create anything that can be created on a wordpress page.
But if you really must you can create a page outside of the database, etc:
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
// I created a custom post type for this plugin called market -- replace post_type with whatever you want
//basically tell wordress to add a query var if sidebar is added to url.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=market','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
This will not be a page that will be in the admin section or in any query that relates to pages but someone could of course navigate to this page. But as i said above you would be better to create a function to create your sidebar. If you want a seperate file to handle the "view" you use require_once 'filename'; a file and keep your functions area free of html.
If you are creating functions in a wordpress plugin dont forget many functions may not be available until later in the load process. Use add_action() if you run into any undefined functions
edit:
you are loading wordpress before you get to the template so you have all the functions. (google wp load for more info) + get_header() / get_footer() will also load a few things like css, etc. I had a small typo in the code above, fixed that but basically what you are doing is telling wordpress if someone lands on www.example.com/sidebar to apply a query_var (rewrite rule). Wordpress will look up its saved vars (final function) and return the template assoc. The 2nd function just registers the var.
You also have wp_functions in any file you create and include in a plugin, etc hence why you can create a file that does exactly the same as this page.

is it possible to make a custom page in buddypress

is it possible to make a custom page for buddypress with its url like this: http://domain.com/custom_page ? I found some answers by searching google but it does not create a custom page. i have a code here that i found in one of the blogs.
define('BP_FUN_SLUG','fun');
function bp_show_fun_page() {
global $bp, $current_blog;
if ( $bp->current_component == BP_FUN_SLUG && $bp->current_action == '' ) {
// The first variable here must match the name of your template file below
bp_core_load_template( 'fun', true );
}
}
add_action( 'wp', 'bp_show_fun_page', 2 );
but this code does not work... Is anyone there knows how to do this? thanks
Yes, it is possible to create a new page in Buddypress.
In Buddypress you have to create a new plugin or create a function in the theme functions file.
For creating first you have to add a new page link in navigation menu using bp_core_new_nav_item() function (You have created sub menu for that use bp_core_new_subnav_item() function).
Above two functions pass the screen function name as a parameter this name use when you click the custom page link call to this screen function. Create new function in your functions.php file same as screen function name. In this function call to your custom template file using bp_core_load_template() function.
Then finish, add more logic to create a new function and call it in the template file.
Another approach is to add a plugin that allows php in posts. For example http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Then create a page and add this to it:
[php] locate_template( array( 'some-template-folder/something.php' ), true ); [/php]
In case someone is wondering how to integrate custom pages into user profile (so that it looks like activity stream, groups etc.).
One thing i did recently was define a plugin (functions.php would work as well), register a custom slug with bp_core_new_nav_item or bp_core_nav_subnav_item and loaded member/single/plugins.php template in the handlers for those slugs. There are a bunch of actions on that page that you can hook into like bp_template_title and bp_template_content.
This can give you a whole lot of control.
Here's a link to the source of plugins.php: http://phpxref.ftwr.co.uk/buddypress/nav.html?readme.txt.source.html

Resources