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';
Related
I want to create a subpage, e.g. /test/ and everything that I enter after /test/ should have one specific template and indexing, no 404 error.
I wanted to make this with virtual pages, but it's too many url's to add. (tried here - Wordpress fake/virtual folder)
I've got my template page-pagename.php which works. Now I need to add that every child of test does not return 404.
I think I have already searched the entire internet and cannot find a solution to this task
What you could fiddle with is with grabbing the main query on 'template_include'. You add an action like this. You can add it to your child theme in functions or in your custom plugin.
add_action( 'template_include', 'custom_router' );
Then in the custom router function, you can check the parameters that you are requesting ('test') and redirect to a template of your choice. Also add in functions.php or in custom plugin. Place a template file in the relevant path.
function custom_router( $query ) {
global $wp_query;
if($wp_query->query['name'] == 'test') :
var_dump($wp_query->query['name']);
var_dump($wp_query->query['page']);
return dirname( __FILE__ ) . '/some-template.php';
endif;
}
I tested the code in the latest wordpress version with a custom plugin and the default theme btw.
I can't seem to find any documentation on this so maybe someone here can help. I have been adding all my WooCommerce hooks to my functions.php file but I would rather have all of them in a separate file to keep things tidy. Obviously I will need another php file for these but how do I link it to the functions.php file so that the code runs as it does now? Thanks
just create new file with your woocommerce hooks code name anything what u want.
Ex. woocommercehook.php and place file in theme folder.
in function.php file just include your woocommercehook.php file at last.
include("woocommercehook.php");
Done. all work same as early.
The PHP include/include_once and require/require_once will work fine. However,
create another file with your hooks( woo-hooks.php ) and use WP get_template_directory to require the file.
require get_template_directory() . '/woo-hooks.php';
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.
I'm building a wordpress theme for someone, based on an older modified theme, and it was using some AJAX scripting with Flash based "holders" for ad areas. I'm trying to use custom name phps to add image widgets to them.
So basically, I am trying to use a <?php get_ad('variantnamehere'); ?> and to get ad-customname.php from my theme files.
I assume I have to register those files within functions.php, but I have no idea how to do that.
Is there any way to do that?
In your theme's functions.php add the following:
require_once( dirname(__FILE__) . '/ad-customname.php' );
Thanks to Artur's feedback, I was able to find what I was looking for, thanks to the article here.
In functions.php, include your files using
include ( dirname(__FILE__) . '/customfilename.php' );
In your theme file where you wish to insert the code within that php file, add:
<?php get_template_part('customfilename');?>
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' );