I am working with wordpress and want to change the output of a shortcode that I wrote myself earlier. Because I forgot where I put it (bad organizing when I started with wp) I wondered whether there is a list of all shortcodes with their source files in the wp database somewhere?
Is there a list that wp uses to connect a shortcode it finds on the page to the fitting source files?
You can use the following code to get a list of all registered shortcodes.
<?php
global $shortcode_tags;
echo '<pre>';
print_r($shortcode_tags);
echo '</pre>';
?>
This will unfortunately not give you the source files of these shortcodes but a simple search through your plugins and themes should reveal the location of the shortcode.
You can print all shortcodes by putting this in a template:
<?php
global $shortcode_tags;
echo "<pre>";
print_r($shortcode_tags);
echo "</pre>";
?>
if you need to include in function.php copy the code to function.php
function add_shortcodes_metaboxes() {
add_meta_box('available_shortcodes', 'Available Shortcodes', 'available_shortcodes', 'page', 'side', 'default');
add_meta_box('available_shortcodes', 'Available Shortcodes', 'available_shortcodes', 'post', 'side', 'default');
}
add_action( 'add_meta_boxes', 'add_shortcodes_metaboxes' );
function available_shortcodes() {
global $shortcode_tags;
foreach ($shortcode_tags as $shortcode_tag => $description) {
echo $shortcode_tag.'<br/>';
};
}
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 try to develop a plugin which includes a file from a template. At this moment my code view is like this:
/* Add a query var for template select, and and endpoint that sets that query var */
add_action( 'init', 'wpse22543_rewrite_system' );
function wpse22543_rewrite_system() {
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'kalkulator_leasingowy', EP_ROOT );
$wp_rewrite->add_rule( '^/kalkulator_leasingowy/?$',
'index.php?template=kalkulator_leasingowy', 'bottom' );
$wp_rewrite->flush_rules();
}
/* Handle template redirect according the template being queried. */
add_action( 'template_redirect', 'wpse22543_select_template' );
function wpse22543_select_template() {
global $wp;
$template = $wp->query_vars;
if ( array_key_exists( 'template', $template ) &&
'kalkulator_leasingowy' == $template['template'] ) {
global $wp_query;
$wp_query->set( 'is_404', false );
include( get_stylesheet_directory().'/kalkulator_leasingowy.php' );
exit;
}
}
function prefix_movie_rewrite_rule() {
add_rewrite_rule( 'kalkulator_leasingowy', 'index.php?template=kalkulator_leasingowy', 'top' );
}
add_action( 'init', 'prefix_movie_rewrite_rule' );
This code runs very fine and includes the template file, but my template (header.php and footer.php) by default uses a Visual Composer and when I use this code on a page, view this:
Visual Composer works good on all pages without a /kalkulator_leasingowy.
How I can include a VC into /kalkulator_leasingowy as well?
File kalkulator_leasingowy.php
<?php
get_header();
?>
<div class="main-wrapper">
<div class="container ">
<div class="row">
</div>
</div>
</div>
<?php get_footer(); ?>
I'm not really understanding where you are trying to render custom Visual Composer code since your template file doesn't have any in it.
But based on your edit, it looks like you might actually want to be using a child theme. These make it very easy to add new template files to the parent theme without editing any of the parent's code and eliminate the need for most of your complex code.
If perhaps you are injecting the Visual Composer code from somewhere else, make sure you are applying the content filters rather than just inserting or echoing to the front end.
$content = '<div id="my_custom_content">[vc shortcode contents here]</div>';
echo apply_filters('the_content', $content);
This will make sure the end content is filtered and rendered appropriately. You might read this related answer for more information.
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]');
This is the blog: http://radutanasescu.ro/
This is the theme: https://wordpress.org/themes/responsive/
The child theme: http://cyberchimps.com/guide/child-theme-example/
And looking here: https://codex.wordpress.org/Customizing_the_Read_More, I tried to convince the theme to show excerpts instead of full posts on the Homepage by typing this in the child themes function.php file like this:
<?php
function new_excerpt_more($more) {
//die('no yolo :(');
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
function child_theme_setup() {
//die('yolo works');
// Replaces the excerpt "more" text by a link
add_filter('excerpt_more', 'new_excerpt_more');
}
add_action( 'after_setup_theme', 'child_theme_setup' );
?>
It's obviously not working because I'm a php noob, if un-commented die('yolo works'); works and die('no yolo :('); doesn't.
i'm trying to show a list of all the sub directories in my multi site network. i've added this function to my theme function.php
function get_all_sites() {
$blog_list = get_blog_list( 0, 'all' );
krsort($blog_list);
foreach ($blog_list AS $blog)
{
echo 'Blog '.$blog['blog_id'].': '.$blog['domain'].$blog['path'].'<br />';
}
}
and added this to the theme header.php
<? get_all_sites(); ?>
But nothing seem to appear. What am i doing wrong?
It looks like is everything okay and it should be working.
I've tested your code here and it's working fine.
But just to advise, you should not be using get_blog_list() it's a deprecated function, since 3.0, you should be using wp_get_sites() instead.
put this code in your functions.php, check your theme, in your question you wrote function.php
function get_all_sites() {
$sites = wp_get_sites();
foreach ($sites as $site) {
printf( 'Blog %d: %s%s <br/>', $site['blog_id'], $site['domain'], $site['path'] );
}
}
Sorry for bad english