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
Related
im developping a wordpress theme and i nedded to add widgets to my header section so i used register_sidebar function on my functions.php :
function my_theme_add_widget_support()
{
register_sidebar(array(
'id' => 'header-2',
'name' => 'the second hader'
));
}
add_action('widgets_init', 'my_theme_add_widget_support');
And then in the file header.php i write this code in the place where i want the widget to display but it doesn't display anything matter of fact i did a test to see if it's active i found out that it's not active at all even though i make sure that i the widgets area i my admin panel the widget have something inside it :
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2')
} else {
echo 'not active';
}
?>
this test always return not active
PS: I'm using the old version of widgets editor
I have created a php function which changes some wp admin menu item names. When I added this code to the function.php of my custom theme it works perfectly. But when I added the same code to a custom plugin, I cannot get all the menu items. I can only edit the main wp admin menu items like posts, pages, settings, plugins, etc. But not the other plugin menu items.
The code:
if (!function_exists('admin_menu_rename')) {
function admin_menu_rename() {
global $menu, $submenu;
if ($menu) {
foreach ($menu as $key => $value) {
if ($value[0] == "originalname") {
var_dump($key);
$menu[$key][0] = "newname";
$menu[$key][6] = "other icon";
}
}
}
}
add_action( 'admin_menu', 'admin_menu_rename' );
}
Why is this code working fine in the themes function.php but not fully functional in the plugin file?
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 currently have a code snippet in my theme's function.php file that allows for a random page to display when you hit the URL www.mydomain.com/random. It looks like this:
add_action('init','random_post');
function random_post() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
But what I would like is for it to apply to only a single category of posts on my site. So something like www.mydomain.com/categories/mycategory/random
PHP isn't my thing, so I don't know what to tweak on this code to make that happen, or if it's even possible as written.
If you want a random post in a specific URL then you can create a page called "Random" so your url will be www.domainname.com/random now create page template
<?php
//Template Name: Template random
//add your random post generate code
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
echo $link = get_permalink($post);
}
?>
And just assign random page template to that new "Template random"
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/>';
};
}