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]');
Related
I'm writing a simple plugin for wordpress that changes a single word on a page or a post to make it bold.
For example: vlbs -> vlbs
It works fine for normal Wordpress pages and posts with this code:
defined('ABSPATH') or die('You can\'t enter this site');
class VLBS {
function __construct() {
}
function activate() {
flush_rewrite_rules();
}
function deactivate() {
flush_rewrite_rules();
}
function unstinstall() {
}
function new_content($content) {
return $content = str_replace('vlbs','<strong style="color:#00a500">vlbs</strong>', $content);
}
}
if(class_exists('VLBS')){
$VLBS = new VLBS();
}
add_filter('the_content', array($VLBS, 'new_content'));
//activation
register_activation_hook(__FILE__, array($VLBS, 'activate'));
//deactivation
register_deactivation_hook(__FILE__, array($VLBS, 'deactivate'));
However, it does not work on a page built with Yootheme Pro Pagebuilder. Whatever is done within the function new_content() is processed after the content has already been loaded. Thus, I cannot manipulate it before it is displayed to the user.
So the question would be: How can I get the content of a page before it is displayed? Is there an equivalent to Wordpress' 'the_content'?
Any help is really appreciated! Thank you very much in advance.
Best regards
Fabian
Yootheme: 1.22.5
Wordpress: 5.2.4
PHP: 7.3
Browser: Tested on Chrome, Firefox, Edge, Internet Explorer
In your code, do you are sure it's the good usage of add_filter content ?
In the doc, the 2nd parameter is string, not array:
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . esc_html__("I'm filtering the content inside the main loop", "my-textdomain");
}
return $content;
}
In wordpress, the_content function display the content. There is an other function for get_the_content
Go to your page file and get the content. You can use str_replace and echo the new content after.
Example single.php :
if ( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post();
$content = get_the_content();
$new_content = str_replace(search, replace, $content);
echo $new_content;
endwhile;
endif;
If it is not possible for you, try to use the output buffer functions. If you need use this functions, I say it and I developpe more this part. But test the solution above before.
Oh, and it exist a special community for WP where your question will more pertinent : https://wordpress.stackexchange.com/
I'm customizing my theme right now. The Standard is, that the logged-in Users Avatar is shown in the Menu bar but I want to display a simple Icon there. I already wright the Code and everything works fine but the Code is located in the mains functions.php File. So in order to make the Site upgradeable, I need to embed the code in the child themes function.php. I found no help there so maybe here is someone who could help me!
Thanks a lot
Below is the Code which I need to embed in the functions.php File of my Child Theme:
add_action( 'init', 'gt3_get_page_id_from_menu' );
function gt3_user_avatar_out($avatar_size=80,$show_email=false){
$user_id = get_current_user_id();
$current_user = get_user_by( 'id', $user_id );
echo "<i class='gt3_login_icon gt3_login_icon--avatar'>";
echo '<i class="fa fa-user"></i>';
echo "</i>";
if ($show_email) {
echo "<span class='gt3_login__user_email'>".esc_html( $current_user->user_email )."</span>";
}
}
If a function is declared in the parent's functions.php, declaring a function with the same name in the child theme will cause a fatal error in the child theme itself. I'd suggest doing something like this instead:
parent function .php
// This can become a filter if you'll need to manipulate the current user
function gt3_get_current_user() {
return get_user_by( 'id', get_current_user_id() );
}
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
$html .= '<i class="gt3_login_icon gt3_login_icon--avatar"><i class="fa fa-user"></i></i>';
if( $show_email ) {
$html .= sprintf( '<span class="gt3_login__user_email">%s</span>', esc_html( gt3_get_current_user()->user_email ) );
}
return $html;
}, 10, 2 );
child theme's function .php
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) {
// $html here is the one that returned from the parent's theme. You can manipulate it or replace the whole output inside here.
return $html;
}, 10, 2 );
Example usages:
echo apply_filters( 'gt3/current_user_avatar', '', false ); // This will echo the icon without the email
echo apply_filters( 'gt3/current_user_avatar', '', true ); // This will echo the icon with the email
echo apply_filters( 'gt3/current_user_avatar', '<p>Append</p>', false ); // This will echo the icon without the email and the '<p>Append</p>' html string before everything else
The result will vary depending on where you use this filter. If you are in the child theme and you altered the html adding the said filter, the result will be customized for that theme. Remember that you can use the use statement to bring in external variables ina closure. For example:
$user_name = 'Mike';
add_filter( 'gt3/current_user_avatar', function( string $html = '', bool $show_email = false ) use ( $user_name ) {
$html .= sprintf( '<p>Hello, %s!</p>', $user_name );
return $html;
}, 10, 2 );
// This will cheer the user, and it will be place at the end of the snippet.
your code is not overwritten by adding a new functions.php file in the same folder, but do not copy/paste the code from the parent theme’s file, because it needs to remain separate from any modifications you make to the child theme. Instead, create a blank file or add any new .php functions required for your child theme
I have a few .html and .php pages in my custom wordpress plugin directory/files that I'm using to output styles JSON/jQuery data basically.
I am wondering how I could essentially wrap these modules in a short code and insert that short code in my Wordpress posts or pages via the Wordpress wysiwyg editor. i.e. [module 1]. I do not want to do this in the theme's files or /functions.php I want to add this functionality from my plugin files, any thoughts?
So, like a php include in the form of a wordpress short code, that works in Wordpress pages.
Here is what I'm trying; within my main plugin php file:
function my_form_shortcode() {
include dirname( __FILE__ ) . 'https://absolute.path.com/wp-content/plugins/my-plugin-v4/assets/files/2019/results/index.php';
}
add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
Within my Wordpress page, I do: (although, does not display/work)
[my_form_shortcode]
You can add your shortcode function the same way you would in your theme
add shortcode function in plugin
Simplest example of a shortcode tag using the API: [footag foo="bar"]
function footag_func( $atts ) {
return "foo = {$atts['foo']}";
}
add_shortcode( 'footag', 'footag_func' );
Example with nice attribute defaults: [bartag foo="bar"]
function bartag_func( $atts ) {
$atts = shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts, 'bartag' );
return "foo = {$atts['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );
Example with enclosed content: [baztag]content[/baztag]
function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
If your plugin is designed as a class write as follows:
class MyPlugin {
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );
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.
I've got wp_link_pages added to the bottom of my posts using this in functions.php:
function custom_pagination( $content ) {
if( is_singular() ) {
$content .= wp_link_pages('echo=0');
}
return $content;
}
add_filter( 'the_content','custom_pagination', 1 );
However this only adds it to the bottom but I'd also like to add them to the top of the post how would I go about achieving this? I'm having to use the function method due to also using jetpack related posts (Which the links appear below it if I don't use this method).
Thanks!
Your script is appending (i.e. .=) the wp_link_pages. Instead, you could do something like:
function custom_pagination( $content ) {
if( is_singular() ) {
$content = wp_link_pages('echo=0') . $content . wp_link_pages('echo=0');
}
return $content;
}
add_filter( 'the_content','custom_pagination', 1 );