I'm training on the creation / modification of wordpress plugin, to do this, I need to make a simple PHP script that performs different actions. The problem is that once the do_action is called, nothing happens.
<?php
[...]
$actions_to_do = [
'wp_ajax_lorem_ipsum'
];
foreach ($actions_to_do as $action) {
$output = do_action($action);
echo $output;
}
I've seen :
Are there any drawback of using output buffer on do_action function?
WordPress: save output of do_action in variable
And tried this trick with the ob_start() but the problem remains the same.
foreach ($actions_to_do as $action) {
ob_start();
do_action($action);
$output = ob_get_contents();
ob_end_clean();
echo $output;
}
I specify that in the state I do not test with a plugin in particular because the problem is the same whatever the plugin or the action
Think of do_action() as a placeholder for a possible action. Here is simple example to illustrate it:
<?php
// You can paste this in page.php, in header of footer, or any other place that would be easy to observe (for test purpose only!)
echo "Hello";
do_action( 'hello_name' );
echo "!";
?>
By simply placing this code and opening a corresponding page you will be presented with:
Hello!
While do_action() was executed, there were no actions associated with it.
Now without removing existing code, add the following to your functions.php file:
<?php
/**
* We register a custom action which will be processed by
* WordPress when do_action( 'hello_name' ) is executed.
*/
function my_name_is() {
echo "Tom";
}
add_action( 'hello_name', 'my_name_is' );
?>
Reloading the page should now display:
Hello Tom!
You may register multiple actions to be triggered when do_action( 'hello_name' ) function is executed using add_action() function.
Related
I am building a theme in woocommerce and I have a need to make a small change to the html output by the woocommerce_output_all_notices() in wc-template-functions.php.
The existing function itself is like so:
function woocommerce_output_all_notices() {
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
What I'd like to do is simply add an id or data attribute and a <span> inside the div, but without editing the code directly (which I clearly don't want to do) I am stumped as it doesn't appear to be a pluggable function and I don't see a way that I can use a hook for this one.
Removing the action that executes the above function should work.
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
then add a new action that returns what you need
function custom_woocommerce_output_all_notices() {
// Change your code here.
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
add_action( 'woocommerce_before_single_product', 'custom_woocommerce_output_all_notices', 10 );
I'm creating a really huge system in PHP and I would like to make things simpler for me and probably for the future developers.
I'm looking to achieve a functionality where when a certain action is triggered in the system, a custom function is called, just the same idea used by WordPress add_action();
This is exactly what I mean,
For example in WordPress, if one needs to execute code in the header section, a hook called wp_head is used, that's
function add_script_to_header(){
echo "this one will be print in the header";
}
add_action( 'wp_head', 'add_script_to_header' );
function another_script_on_header(){
echo "this onather script on the header";
}
add_action( 'wp_head', 'another_script_on_header' );
Now that was just an example, I would like to implement the same thing with custom PHP completely independent of WordPress, which means, my code is not in any way related to WordPress.
I've tried to use callable functions in PHP, but at some point, it gets tongue-twisting
In my case, I have a class called Orders that records, payments, shift orders from one state to another, and a lot more things, I would like to have a hook or way of triggering action with a function when certain actions are triggered.
The following code indicates some of my trials
function next_order($orderId, $status, $autoApprove = 0){
// this is function that is responsible for shifting orders from one status to another
// or from one state to another
// I would like add_action() function to be linked here, so that I can track
// order status with external functions
}
function add_action( $action_name, $function_name ){
if( $action_name == "order_next" ){
call_user_func( $function_name, $action_name);
}
if( $action_name == 'order_placed' ){
// here we can place an action for that
}
if( $action_name == 'order_paid' ){
}
}
function benson_karue( $orderId ){
echo "This is the order id ".$orderId;
}
add_action( 'order_next', 'benson_karue' );
I'm trying to simply show content with a php if statement.
I would like to add a footer element (to the footer.php) for the homepage only.
So, if body tag has class 'home'; then add new footer element. I'm trying below; but currently nothing is displaying at all.
$classes = get_body_class();
if (in_array('home',$classes)) {
function lastUpdated() {
echo '<div class="last-updated">Last Updated:<span class="date-update"></span></div>';
}
}
the above code is in my child theme's functions.php file;
First of all, how WP will know, where you want to output that markup?
To solve this question you can use hooks, you need to monitor parent theme for such code do_action('before_theme_footer'), to find place where you can "inject" your code.
And then you can apply your output to this hook, and inside callback function check if you on home page with is_front_page():
function footer_markup(){
if( is_front_page() ) {
echo '<div></div>';
}
}
add_action('before_theme_footer', 'footer_markup');
You can read about hooks here - https://designmodo.com/wordpress-hooks-filters/
If no any hooks in footer.php, you can copy footer.php to child theme and add your code in the right place where you want to output it:
if( is_front_page() ) {
echo '<div></div>';
}
I have a page where I need to allow the user to enter a paragraph of text. Then after that text, insert a shortcode that will render a list of posts, then add more free-form text after that. My thought was that they should be able to insert a shortcode which will output the posts. This way they can simply add the shortcode where they wish the posts to appear.
I currently have the logic which retrieve the posts separated in its own file. Currently I include it in a page by simply using the get_template_part() function:
get_template_part('donation', 'posts');
I looked into how to create a shortcode and included the following code into my functions.php file in order to create the shortcode:
add_shortcode('donation-posts', 'fnDonatePosts');
function fnDonatePosts($attr, $content)
{
get_template_part('donation', 'posts');
}
The donation-posts.php is being properly executed and the posts are appearing, however, they are always positioned BEFORE the content and not in the location that the shortcode was placed.
I have tried removing the get_template_part() function and just output some text and that works fine. So I understand that the get_template_part() may not be the right way to do this, however, I have not been able to uncover a way to do what I am attempting to do (I am sure that there is a way... I just haven't found it).
I have tried:
include(get_template_directory(). '/donation-posts.php');
include_once(get_template_directory(). '/donation-posts.php') :
But these stopped processing once they hit the PHP code in the included file.
I have also tried:
$file = file_get_contents(get_template_directory(). '/donation-posts.php');
return $file;
But this only returns the contents of the file (as the function name indicates) which means it doesn't execute the PHP script to return the posts.
Anybody out there done this before?
You may try this, it may solve your problem because get_template_part basically reacts like PHP's require, it doesn't return but echos the content immediately where it's been called.
add_shortcode('donation-posts', 'fnDonatePosts');
function fnDonatePosts($attr, $content)
{
ob_start();
get_template_part('donation', 'posts');
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
Here is a more dynamic version where you can pass the path to the template.
function template_part( $atts, $content = null ){
$tp_atts = shortcode_atts(array(
'path' => null,
), $atts);
ob_start();
get_template_part($tp_atts['path']);
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
add_shortcode('template_part', 'template_part');
And the shortcode:
[template_part path="includes/social-sharing"]
Minimal version of the accepted answer:
function my_template_part_shortcode() {
ob_start();
get_template_part( 'my_template' );
return ob_get_clean();
}
add_shortcode( 'my_template_part', 'my_template_part_shortcode' );
where my-template.php is the file you'd like to include.
get_template_part() didn't work for me when using it in functions.php. I used locate_template() inside the ob_start and clean instead. For example:
function full_petition_shortcode( $attr ) {
ob_start();
locate_template( 'petition.php', TRUE, TRUE );
return ob_get_clean();
}
add_shortcode( 'full-petition', 'full_petition_shortcode' );
I'm trying to add a code to the_content() function. I tried the following
function add_something($content) {
echo "test";
}
add_filter( 'the_content', 'add_something', 6);
After adding the filter my posts return just the echo test without the content. How can I execute my custom code without omitting the content?
I would surmise you need something like that (if it's indeed a filter callback):
function add_something($content) {
return "test" . $content;
}
Seems what the docs say:
http://wordpress.org/support/topic/add-something-to-the_content
you omitted the return statement.
function add_something($content) {
echo "test";
... change $content ......
return $content;
}
Be advised that if you want to modify the content, you must append it to the variable. Using echo will output 'test' at the time the script is called. It will not append or prepend it to the_content()
Use this (the_content filter needs 1 parameter)
add_filter( 'the_content', 'add_something', 1);