I have successfully added a content after short description on single product page with
if (!function_exists('my_content')) {
function my_content( $content ) {
$content .= '<div class="custom_content">Custom content!</div>';
return $content;
}
}
add_filter('woocommerce_short_description', 'my_content', 10, 2);
I saw that in short-description.php there was apply_filters( 'woocommerce_short_description', $post->post_excerpt )
so I hooked to that.
In the same way, I'd like to add a content after the add to cart button, so I found do_action( 'woocommerce_before_add_to_cart_button' ), and now I am hooking to woocommerce_before_add_to_cart_button. I'm using
if (!function_exists('my_content_second')) {
function my_content_second( $content ) {
$content .= '<div class="second_content">Other content here!</div>';
return $content;
}
}
add_action('woocommerce_after_add_to_cart_button', 'my_content_second');
But nothing happens. Can I only hook to hooks inside apply_filters? From what I've understood so far by working with hooks is that you only need a hook name to hook to and that's it. The first one was a filter hook, so I used add_filter, and the second one is action hook so I should use add_action, and all should work. So why doesn't it?
Here, you need to echo content as it is add_action hook.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
/*
* Content below "Add to cart" Button.
*/
function add_content_after_addtocart_button_func() {
// Echo content.
echo '<div class="second_content">Other content here!</div>';
}
You need to do echo instead of return.
add_action( 'woocommerce_after_add_to_cart_button', 'ybc_after_add_to_cart_btn' );
function ybc_after_add_to_cart_btn(){
//add text OR HTML here
echo '<p>After custom text here</p>';
}
If you want the same thing on the shop archive page then you need to use the woocommerce_loop_add_to_cart_link filter to modify the add to cart button.
When using 'Action Hook' adding the content(html) out of php would be easy.
if (!function_exists('my_content_second')) {
function my_content_second( $content ) {
?>
<div class="second_content">Other content here!</div>;
<?php
}
}
add_action('woocommerce_after_add_to_cart_button', 'my_content_second');
If need to add dynamic content just echo that content using variables or add using some condition.
Filter hooks are useful to modify the existing content and needs a return statement (the modified)
Action hooks are mostly useful to add content.
Related
So there is a code snippet at businessbloomer to add a edit cart link to checkout page, however it does not put it in a good placement by default so needs some CSS. I'd like to be able to add a class or id to the snippet so that it is easier to reference, can you advise how to add such to this echo statement, everything I've tried results in an error.
Here is the code:
add_action( 'woocommerce_checkout_before_order_review', 'bbloomer_edit_cart_checkout' );
function bbloomer_edit_cart_checkout() {
echo 'Edit Cart';
}
Again, proper credit to https://businessbloomer.com, this is their code not mine. ;)
add_action( 'woocommerce_checkout_before_order_review', 'bbloomer_edit_cart_checkout' );
function bbloomer_edit_cart_checkout() {
echo 'Edit Cart';
}
add_action( 'woocommerce_checkout_before_order_review', 'bbloomer_edit_cart_checkout' );
function bbloomer_edit_cart_checkout() {
echo '<a class="your-class-here" id="your-id-here" href="'.wc_get_cart_url().'">Edit Cart</a>';
}
simple. done.
I would like to replace some text with photo in Buddypress welcome email.
I'm new to Wordpress and filter.
<?php
do_action( 'bp_before_email_header' );
echo bp_get_option( 'blogname' );
do_action( 'bp_after_email_header' );
?>
I add a code on function.php like this:
add_action( 'bp_before_email_header', 'add_logo');
function add_logo() {
echo '<img src="photo url"';
}
There's a photo in email header, however, I would like to remove the text bp_get_option( 'blogname' );
If there's no apply filter, I can not modify it without editing it directly?
How can I do that?
Thank you for your help!
bp_get_option has a filter. Check Code. You need simply apply this filter like that for example.
add_filter('bp_get_option', function($value) {
if ($value == 'blogname') {
return '';
}
return $value;
}, 10);
I have this PHP script:
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
What is the shortcut i need for the page title to be shown? What do i need to put in the HTML for the fetched title to be shown
Add this to your theme, or make an plugin from it.
/* title to get the post title */
function getPageTitle() {
global $wp_query;
return get_post_title($wp_query->post->ID);
}
/* Add shortcode */
add_shortcode('page_title', 'getPageTitle');
for more ShortCode Api
You may use this.
echo do_shortcode('[page_title]');
https://developer.wordpress.org/reference/functions/do_shortcode/
I've created a theme from scratch and I have issues creating shortcodes. I have the following code:
functions.php
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
in the WP Admin page editor:
[caption]My Caption[/caption]
on the page template page:
echo do_shortcode('[caption]');
The shortcode seems to be somehow working as it returns the HTML but not the $content.
My problem is that I can't seem to get my hand on the $content and display it using the shortcode. Any idea why this is happening?
P.S. I don't want to use the_content() function to display all the content, I want to use the shortcodes to divide the content the user adds in several pop-ups and child sections of the page.
Thanks!
Make sure you user shotcode same page
// [baztag]content[/baztag]
function baztag_func( $atts, $content = '' ) {
return $content;
}
add_shortcode( 'baztag', 'baztag_func' );
echo do_shortcode('[baztag]');
I am making a plugin, to register a shortcode, which when used like this: [append_css mycss] it will look for a custom field called mycss and add the contents to the head of the document. It all works great, except that the code is being added to the body, and I don't know how to get it to add to the head.
I have tried adding an action wp_head but I don't know how to pass variables whilst doing that, and it does not seem to fire from within the shortcode callback anyway.
function append_css_short($params){
global $post;
if(sizeof($params)){
$key = $params[0];
} else {
$key = 'css';
}
return '<style type="text/css">'.
get_post_meta($post->ID, $key, true)
.'</style>';
}
add_shortcode('append_css','append_css_short');
How would I get this to write to the head instead of body?
Is there a better approach to the problem?
If you want to print something to the head of the document you need use add_action with wp_head hook.
A better approach might be to not use a shortcode at all. Shortcodes are intended to add content or modify content. Instead create a function that you can attach to wp_head that will print your css. You may want to force the user to keep the custom field name consistent. Or better yet have your plugin add a metabox to posts that they can enter custom css into. Then you will always be certain of the name of the field.
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'mypluginname_css', true ) ) {
return;
}
return '<style type="text/css">'. get_post_meta($post->ID, $key, true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
The only thing i am not certain of is whether or not $post with the custom field data will be available outside the loop. So you may have to add some extra querying in there to pull the custom data at the time that wp_head is fired.
With some minor adjustments to the answer provided by #Jrod, here is the complete working plugin, with usage as follows:
Create custom field called append_css (or append_js) with required code
... of course, there is no number 2 ;)
Code
<?php
/**
* #package Append_CSS_JS
* #version 0.1
*/
/*
Plugin Name: Append CSS JS
Plugin URI: http://itaccess.org/wordpress-plugins/append-css-js/
Description: Append Css or Js to any page using Custom Fields
Author: Billy Moon
Version: 0.1
Author URI: http://billy.itaccess.org/
*/
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_css', true ) ){
return;
}
echo '<style type="text/css">'. get_post_meta($post->ID, 'append_css', true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
function append_js() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_js', true ) ) {
return;
}
echo '<script type="text/javascript">'. get_post_meta($post->ID, 'append_js', true) .'</script>';
}
add_action( 'wp_head', 'append_js' );
?>
I don't think this is possible that way, because shortcodes will be called after wp_head(), so you cannot hook on it.
Here's the list of all wordpress hooks http://adambrown.info/p/wp_hooks
EDIT:
Here's a workaround:
In your template:
<?php
//get the content of your post:
if(have_posts()) while(have_posts()) the_post();
$pcontent = get_the_content();
endwhile;
?>
<html>
<head>
...
</head>
<body>
...
<?php echo $pcontent; ?>
</body>
</html>