program custom wordpress plugin to output only to a specific page - php

I need to know how to take the output of a custom built Wordpress plugin and output it onto a specific page.
I know that I need to use the add_action() / add_filter() functions to call the function which outputs the plugins output when a wordpress hook function runs.
Currently I am using the 'the_content' hook.
This outputs my plugins output to all pages in my theme which call the the_content() function.
Which hook can I use to make the output only appear on a specific page.
Also
It would be useful to know how to create a page using my plugin.

Checkout is_page()
if (is_page(123)) {
// put your hooks here
}

put your code in a file inside plugin directory
then
use this
function page_directory()
{
if(is_page('123')){
$dir = plugin_dir_path( __FILE__ );
include($dir."custom/page.php");
die();
}
}
add_action( 'wp', 'page_directory' );

Related

Plugin-file minimum requirements for hooking into wordpress?

I'm relatively new to working with WP. In my main plugin file, I have a couple of hooks working. I have another file that I'm trying to add an add_action() to, but it keeps coming back as undefined.
What are the basic requirements for a file to hook into WP?
add_action('init', 'loop_users');
function loop_users() {
$users = get_users();
//execute code here
}
When i use this add_action or any other WP hook it always returns as undefined. Thoughts?
You can include your other files to your main plugin file using include statement. For example if your main plugin file and another file are in the same directory.
include dirname( __FILE__ ) .'/yourfile';

Call function from main WordPress theme in a plugin

I have a function in my theme functions.php file which returns a value:
function my_theme_function() {
return "100";
}
Anywhere in my theme templates I can simply do this...
echo my_theme_function()
...and I see the number 100 on the page. That's cool.
But in my plugin I would have expected to be able do also get access to this function by echoing my_theme_function() but instead I get a 'call to undefined function' error.
The strangest part is I'm certain this was working a couple of days ago, but I've not touched the code since. I suspect some WordPress shenanigans, but I don't know why or how to get around this.
The reason you may take this result can be the order in which the theme and the plugins are loaded.
For example, your plugin can get loaded before the theme, and obviously, in this case, the function it is not available in your plugin source code.
The solution to this issue are the WordPress Hooks. I don't know what is your plugin code style, but you can bootstrap your plugin in the init hook or even better in the after_setup_theme.
So for example, let's say, you need your plugin should run once your theme is loaded by the WordPress. You can use the following code to do so:
function my_theme_is_loaded() {
// Bootstrap your plugin here
// OR
// try to run your function this way:
if ( function_exists( 'my_theme_function' ) ) {
my_theme_function();
}
}
// You can also try replace the `after_setup_theme` with the
// `init`. I guess it could work in both ways, but whilw your
// plugin rely on the theme code, the following is best option.
add_action( 'after_setup_theme', 'my_theme_is_loaded' );
What the above code does, is like you say to your plugin, wait until the theme is totally loaded, and then try to run my plugin code that rely on the theme code.
And of course, I suggest either wrap your theme function in a plugin function like that:
// This way, your plugin will continue running even if you remove
// your theme, or by mistake your rename the function in the theme
// or even if you totally decide to remove the function at all in the
// side of the theme.
function function_from_theme() {
if ( function_exists( 'my_theme_function' ) ) {
return my_theme_function();
} else {
return 0; // Or a value that is suitable with what you need in your plugin.
}
}
This is going to protect your site against theme de-activation or theme change. In this cases, you are going to have a plugin looking for a function in your theme, and while you change the theme or deactivate your theme, your plugin will break your site.

Wordpress plugin add page frontend

I'm developing a plugin for wordpress. This plugin has to have an admin section for plugin set up but also has to have a custom front end page with forms.
I'm new into the wordpress plugin development world but I have not found specific information for this task.
Is there a way to add pages to front end from a plugin or is necessary to manually edit the current template and add the page?
Here is a way to add custom content on a front-end page when you create your plugin: http://blog.frontendfactory.com/how-to-create-front-end-page-from-your-wordpress-plugin/
function elegance_referal_init()
{
if(is_page('share')){
$dir = plugin_dir_path( __FILE__ );
include($dir."frontend-form.php");
die();
}
}
add_action( 'wp', 'elegance_referal_init' );
To add a page to Wordpress that runs custom code without adding the page as a post or page in the database, try this in your plugin file.
add_action ('pre_get_posts', function ($query) {
global $wp;
if ($wp->request == 'pageurl'){
$dir = plugin_dir_path( __FILE__ );
include $dir . "your_file.php";
die();
}
});
I think you looking for this for the admin settings page https://codex.wordpress.org/Function_Reference/add_options_page
Fot the front end, i would recommend to create a page with a template https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

Enqueue Different Stylesheet in wordpress depending on user input via meta box

I am creating a wordpress plugin from which user can select different styles from the plugin options panel. I want to enqueue css file via wp_enqueue_style() according to the user input.
I am getting error when I want to get the metabox value inside the plugins php file.
Error Says:
Calling undefined variable $... on line no ..."
Is there any way I can get the value of the metabox inside php file and enqueue the appropiate css file according to the user input on dashboard.
I am using Custom-Metaboxes-and-Fields-for-WordPress plugin to retrieve user input from plugin option panel for this purpose
My code:
function easyloader_theme() {
global $post;
$theme = easyloader_get_option( 'easyloader_theme' );
wp_register_style('easyloader_style', plugins_url('themes/pace-theme-'. $theme .'.css',__FILE__),'','1.0.0.', false);
wp_enqueue_style('easyloader_style');
}
add_action( 'init', 'easyloader_theme');
I can easily get value of this user input in any when I paste this code in themes php file like (header.php, page.php, footer.php ) via
<?php global $post; $theme = easyloader_get_option( 'easyloader_theme' ); echo $theme; ?>
I just want to get the value inside my plugin php file not in the theme file.
The proper way to enqueue is with the following action:
add_action( 'wp_enqueue_scripts', 'easyloader_theme' );
function easyloader_theme() {
/* Enqueue Scripts AND Styles */
}
Looks like the plugin has some internal cache, but at the end you can try using WP function directly:
get_option( 'easyloader_theme' );

Want to overwrite functions written in woocommerce-functions.php file

I want to modify/overwrite functions written in woocommerce-functions.php file but I don't want to modify woocommerce-functions.php file. That is I want to achieve this in plug-in or in my theme.
It is possible to override woocommerce functions, I did this recently and added all of my woocommerce extended functionality to my theme's functions.php file so that the woocommerce plugin files remained untouched and are safe to update.
This page gives an example of how you can remove their action and replace it with your own -
http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp
This page gives an example of extending upon their functions without removing their function, as well as using child themes -
http://uploadwp.com/customizing-the-woocommerce-checkout-page/
Hope this helps :)
WooCommerce provides a templating system. It is possible to override woocommerce functions. great way to customize WooCommerce without modifying core files, is to use hooks -
If you use a hook to add or manipulate code, you can add your custom code to your theme functions.php file.
Using action hooks -
To execute your own code, you hook in by using the action hook do_action(‘action_name’);.
See below for a great example on where to place your code:
add_action('action_name', 'your_function_name');
function your_function_name()
{
// Your code
}
Using filter hooks-
Filter hooks are called throughout are code using apply_filter(‘filter_name’, $variable);
To manipulate the passed variable, you can do something like the following:
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable )
{
// Your code
return $variable;
}
Here you can get WooCommerce Action and Filter Hook - https://docs.woothemes.com/wc-apidocs/hook-docs.html
If you have a child theme, you can copy the relevant file to your theme and rewrite the copy. The copy will be used in preference to the WooCommerce version.
I was needed to add "Play" button for videos on mobile devices (by default this button is shown just on desktop).
I was needed to override the function in wp-content/themes/gon/framework/theme_functions.php:
function ts_template_single_product_video_button(){
if( wp_is_mobile() ){
return;
}
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
I found this instruction which states If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.
I already had wp-content/themes/gon-child/functions.php, (ie the original gon theme had been copied to gon-child), so what I did was:
// Enable tour video on mobile devices
remove_action('ts_before_product_image', 'ts_template_single_product_video_button', 1);
add_action('ts_before_product_image', 'ts_template_single_product_video_button_w_mobile', 1);
function ts_template_single_product_video_button_w_mobile(){
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
?>

Categories