I'm trying to add customization to a Wordpress theme. Instead of replacing the original code, I'm using a "child theme" technique where I can replace files individually.
Now, that original code invokes the get_footer(), and I like to intercept that call so that I can insert my own html output before the actual get_footer function does its work.
I've read about the PHP 5's __call method but that only applies to classes whereas the WP code is not using classes but global functions. So I cannot use that technique, right?
get_footer() function just find and load proper template for the footer. There is a hook in WP get_footer that rans just before template being loaded.
Codex says that is not good to echo-es from this function, but you can use it to load your own template just before template that get_footer() loads.
You can do something like this in your functions.php:
add_action( 'get_footer', 'my_hook_fn' );
function my_hook_fn() {
load_template('my_template_file.php');
}
Codex
I use that thick in several sites, they do not use child themes, but I think this will do the trick.
Related
How can I overwrite files in the parent theme with the replacement versions in my child theme? We are using Divi as the parent theme.
We were told, in our functions.php you would create a function with all our modifications, then tell wordpress to hook our function in when et_builder_structure_elements_load runs. Im extremely new to wordpress and php so any help would mean a lot.
You need to first add the child theme and activate it. If you haven't read it already, here is a link to the wordpress documentation to do this: https://developer.wordpress.org/themes/advanced-topics/child-themes/#how-to-create-a-child-theme
Note that you cannot remove any function from the parent theme. The parent functions and child functions will run together.
Then you can add an action hook to your functions.php of the child theme like so:
<?php
add_action('et_builder_structure_elements_load',function(){
echo "Doing my function";
})
This will run when wordpress calls do_action('et_builder_structure_elements_load')
I am attempting to create a WordPress plugin and ran into a road block. I am creating a settings page that will need to be extended by child classes. The main class will look something like this.
class SettingsPage {
// Properties
// Methods
public function metaboxes() {
add_meta_box( //metabox params );
}
}
This class will handle the main settings page. I now want to be able to crete meta boxes from within several child classes. Basically extending/appending to the metaboxes() method.
class MetaBoxOne extends SettingsPage {
public function metabox() {
// add another metabox
}
}
What would be the best solution for this problem? I've been staring at my code editor for a few hours trying to figure it out with no luck. Any help would be greatly appreciated.
Is inheritance the best route?
This design can use a bit more Composition. Also having IMetaBox (interface) defined to ensure that your page classes will have functionality, that will allow you to adjust their content and behavior. So your settings page can look like this:
class SettingsPage {
// Fields
ICollection<IMetaBox> _metaBoxes;
// Properties
// Methods
public function metaboxes() {
add_meta_box( //metabox params );
}
}
All your
that will need to be extended by child classes
will have the build-in functionality of a ICollection<T> that you'll need to manage the metaboxes, what is left to do is call a display method that'll iterate over the collection.
Update:
Maybe this article will helpful for you:
http://code.tutsplus.com/articles/integrating-with-wordpress-ui-meta-boxes-on-custom-pages--wp-26843
Firstly, if you want to create a settings page for you plugin, I recommend you to use the built in Settings API. This tutorial teach you everything what you need to catch up the concept of this API.
Secondly, I think you couldn't use metaboxes on custom settings page, because the 'add_meta_boxes' hook only fires on edit post pages, see that in the codex:
Advanced Actions This section contains actions related to the queries
WordPress uses to figure out what posts to display, the WordPress
loop, activating plugins, and other fundamental-level WordPress code.
add_meta_boxes Runs when "edit post" page loads. (3.0+)
Finally: In other case if you have a main container, which contains child entities and you want to collect the child entities in your main container, I think you should follow the core Wordpress pattern, the callback action hooks. In your main collector you must define a custom action hook with:
do_action('your_plugin_main_collector_do_something', $param1, $param2, $param3, ...);
And your child entities should register themselves to the main container with this:
add_action('your_plugin_main_collector_do_something', 'my_entity_callback_function_name', $priority_default_10, $how_many_parameters_you_have_to_use);
Am having a plugin, In that I need to change some text it in, but this function doesn't have hook to use.
Usually the function will be override by using remove_action() or remove_filter(). For both the function we need filter name to override. But this plugin function doesn't add any filters.
Now I need to override this function.
For example
Plugin.php in plugin
function plugin(){
echo 'hello';
echo 'welcome you';
}
Function.php in theme
I want to the function plugin() in plugin.php to
function theme_plugin(){
echo 'hello';
echo 'You are welcome';
}
There is not add_action for plugin().
How to override the plugin() to theme_plugin()?
Look here Redefining PHP function?, you can't override php function or overload it. You have to find another way for solving your problem.
Regards,
If am right, you can us this link: http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/
**** Edit**
I have no much experience with wordpress. But here is someone asking for the same: Overriding a theme function from plugin in WordPress
And the marked answer is:
Unless the function is meant to be overridden, no. This is basic PHP. You can't redefine a function. If you try you will get a fatal error.
Parts of WordPress are written to be overwritten. Look at /wp-includes/pluggable.php. Every function in there is wrapped in a if( !function_exists(...) ) conditional. Unless your theme did the same, and some do for some functions, you can't overwrite.
Look around for filters that might help you instead.
Looking at your code, you should be able to unhook that. Just make sure to hook the unhook late enough. That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using.
Is there something in $fragments, or in $_POST or $_GET or anything else, that you can use to conditionally run your code, leaving the rest alone.
Anyway, Wordpress is opensource, so you can change the code by yourself? Why just change the plugin?
I have tried making a function which I can run in the .tpl files.
I included a class Templates:
<?php
class Templates {
function getTemplate($template, $gameid) {
echo "test";
}
}
?>
Then I put this into the PHP page.
//Create a template object
$templates = new Templates();
$tpl->assign("template", $templates);
And then I try calling the function in the tpl file:
{$template->get('header',1)}
But I just get a blank page when I try this.
Any ideas how to actually do this?
You should probably look through the documentation on Smarty Plugins. These allow you to register your own functions which can be used just like the built-in tags and modifiers, or even custom sources of template data for use with the {include} function.
Your example is too stripped-down to guess what you actually want to achieve, so feel free to post a follow-up question if you can't see a way of achieving your specific goal.
Something that has been bothering me since I started using drupal is how exactly does the php engine know which $node/$classes/$attributes you are referring to in template files. these variables are never declared as globals, so how does the php engine figure out what "$node" you are referring to??
See http://drupal.org/node/223430 which states
The main role of the preprocessor is to set up variables to be placed
within the template (.tpl.php) files. From Drupal 7 they apply to
templates and functions, whereas in Drupal 6 preprocess functions only
apply to theming hooks implemented as templates. Plain theme functions
do not interact with preprocessors.
Look at the template_preprocess* and the template_process referred to in the API. These functions show you the code that set up the various variables that you can access in your node template.
In a similar manner, you can also add your own preprocess code in a theme function to add to the variables available in your tpl.php file like so:
/**
* Implements hook_preprocess_HOOK().
*/
function MY_MODULE_preprocess_node(&$variables) {
$variables['hello'] = 'Look at me now!';
}
After this, you can reference $hello in your node.tpl.php file.
These variables are set by Drupal's templating engine.