admin管理员组文章数量:1122846
I've created a widget for WP 5.9.3, based on an online tutorial, which I'd got working okay, but it relied on having all the code in the child theme's functions.php, which started looking a bit messy.
So, I've put all the code into a separate file, in the base directory of the child theme, and tried to require
it from within the child theme's functions.php
.
However, now it doesn't seem to work, or get registered, at all. After several hours of tinkering, it still doesn't seem to be recognized at all. I've looked at a ton of other questions, but I can't find anything which works. The site loads still, but the widget is not visible either on the site or in the Admin area.
Among the things I've tried putting into functions.php, besides hard-coding (last resort) the path, are the following:
1) require_once('my_widget1.php');
2) define('MY_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__)));
require_once (MY_THEME_FOLDER_PATH . 'my_widget1.php');
3) require_once( get_stylesheet_directory() . '/my_widget1.php');
The actual path to the widget file is:
/var/www/sites/xyz/wp_main/wp-content/themes/mychildtheme/my_widget1.php
I haven't used WP very much, but I'm mystified as to why it doesn't work. I guess I'm missing something obvious, but I'm not sure how to make further progress. Any suggestions and assistance would be much appreciated.
Per the request in the comments, here's the widget code (just minus the output bits). It's basically an example from WPB which I modified, and, as noted, already does work fine when the code is actually inside functions.php. Linux permissions 644.
<?php
// Widget Creation
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
$post_id = get_the_ID();
# ... then a bunch of stuff to output
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
// Class wpb_widget ends here
}
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
?>
If it's of any relevance, the child theme I'm using is derived from the momentous-lite
theme.
I've created a widget for WP 5.9.3, based on an online tutorial, which I'd got working okay, but it relied on having all the code in the child theme's functions.php, which started looking a bit messy.
So, I've put all the code into a separate file, in the base directory of the child theme, and tried to require
it from within the child theme's functions.php
.
However, now it doesn't seem to work, or get registered, at all. After several hours of tinkering, it still doesn't seem to be recognized at all. I've looked at a ton of other questions, but I can't find anything which works. The site loads still, but the widget is not visible either on the site or in the Admin area.
Among the things I've tried putting into functions.php, besides hard-coding (last resort) the path, are the following:
1) require_once('my_widget1.php');
2) define('MY_THEME_FOLDER_PATH', trailingslashit(get_template_directory(__FILE__)));
require_once (MY_THEME_FOLDER_PATH . 'my_widget1.php');
3) require_once( get_stylesheet_directory() . '/my_widget1.php');
The actual path to the widget file is:
/var/www/sites/xyz/wp_main/wp-content/themes/mychildtheme/my_widget1.php
I haven't used WP very much, but I'm mystified as to why it doesn't work. I guess I'm missing something obvious, but I'm not sure how to make further progress. Any suggestions and assistance would be much appreciated.
Per the request in the comments, here's the widget code (just minus the output bits). It's basically an example from WPB which I modified, and, as noted, already does work fine when the code is actually inside functions.php. Linux permissions 644.
<?php
// Widget Creation
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
$post_id = get_the_ID();
# ... then a bunch of stuff to output
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
// Class wpb_widget ends here
}
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
?>
If it's of any relevance, the child theme I'm using is derived from the momentous-lite
theme.
1 Answer
Reset to default 0Okay, so after a couple of days pulling my hair out, I finally realized that I needed to add the following line, after the require_once
line in functions.php:
add_action('widgets_init', 'wpb_load_widget');
... and everything then worked perfectly. (where wpb_load_widget
is the name of my register-widget function in the widget file itself.)
本文标签: Include widget file in functionsphp of child theme
版权声明:本文标题:Include widget file in functions.php of child theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289996a1928421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
require_once get_stylesheet_directory() . '/myfile.php'
version at the beginning offunctions.php
after adefined( 'ABSPATH' )
check, without issue ; could you give us the code inmy_widget1.php
(or a version of it without the main logic if it's sensitive) so that we can actually test it out? – froger.me Commented Apr 13, 2022 at 2:43