我非常感激。如果有人帮我的话。我使用的是最新版本的WordPress。我正在尝试创建一个小部件,显示公司的名称和联系信息。从代码中,我只能添加一个公司的详细信息,但无法添加更多的公司的详细信息,当我点击添加另一个公司按钮,它抛出一个弹出显示,显示离开网站?您所做的更改可能无法保存。这里我分享子主题functions.php文件和custom-widget.php文件。在最后的functions.php文件中,我包含了custom-widget.php文件。
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array() );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
require_once('custom-widget.php');
// END ENQUEUE PARENT ACTION
这是custom-widget.php文件代码:
<?php
// Creating the widget
/* Create Widget Start Here */
/**
* Adds ContactInformation_Widget widget.
*/
class ContactInformation_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'contactinformation_widget',
__( 'Contact Information Widget', 'contact-information-widget' ),
array( 'description' => __( 'Display your company name(s) and contact information.', 'contact-information-widget' ), )
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$output = '';
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
}
$company_names = isset( $instance['company_names'] ) ? $instance['company_names'] : array();
$contact_emails = isset( $instance['contact_emails'] ) ? $instance['contact_emails'] : array();
if ( ! empty( $company_names ) || ! empty( $contact_emails ) ) {
$output .= '<div class="ciw_contactinformation">';
foreach ( $company_names as $index => $company_name ) {
if ( ! empty( $company_name ) || ! empty( $contact_emails[ $index ] ) ) {
$output .= '<div class="ciw_company_information">';
if ( ! empty( $company_name ) ) {
$output .= '<div class="ciw_company_name">
<div class="ciw_company_name_text">' . $company_name . '</div>
</div>';
}
if ( ! empty( $contact_emails[ $index ] ) ) {
$output .= '<div class="ciw_email">
<i class="fa fa-envelope"></i>
<div class="ciw_contact_email"><a href="mailto:' . $contact_emails[ $index ] . '" target="_blank">' . $contact_emails[ $index ] . '</a></div>
</div>';
}
$output .= '</div>';
}
}
$output .= '</div>';
}
echo $output;
echo $args['after_widget'];
}
/**
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Contact Us', 'contact-information-widget' );
$company_names = isset( $instance['company_names'] ) ? $instance['company_names'] : array();
$contact_emails = isset( $instance['contact_emails'] ) ? $instance['contact_emails'] : array();
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_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>
<div id="<?php echo $this->get_field_id( 'company_information' ); ?>">
<?php
if ( ! empty( $company_names ) || ! empty( $contact_emails ) ) {
foreach ( $company_names as $index => $company_name ) {
?>
<div class="ciw_company_information_container">
<label><?php echo __( 'Company Name:' ); ?></label>
<input class="widefat" name="<?php echo $this->get_field_name( 'company_names' ); ?>[]" type="text" value="<?php echo esc_attr( $company_name ); ?>">
<label><?php echo __( 'Contact Email:' ); ?></label>
<input class="widefat" name="<?php echo $this->get_field_name( 'contact_emails' ); ?>[]" type="email" value="<?php echo esc_attr( $contact_emails[ $index ] ); ?>">
</div>
<?php
}
} else {
?>
<div class="ciw_company_information_container">
<label><?php echo __( 'Company Name:' ); ?></label>
<input class="widefat" name="<?php echo $this->get_field_name( 'company_names' ); ?>[]" type="text" value="">
<label><?php echo __( 'Contact Email:' ); ?></label>
<input class="widefat" name="<?php echo $this->get_field_name( 'contact_emails' ); ?>[]" type="email" value="">
</div>
<?php
}
?>
</div>
<p>
<button class="button add_new_company"><?php echo __( 'Add Another Company' ); ?></button>
</p>
<script>
jQuery(document).ready(function($) {
var container = $('#<?php echo $this->get_field_id( 'company_information' ); ?>');
var addButton = container.find('.add_new_company');
var index = <?php echo count( $company_names ); ?>;
addButton.on('click', function(event) {
event.preventDefault();
index++;
var companyInformation = '<div class="ciw_company_information_container">';
companyInformation += '<label><?php echo __( 'Company Name:' ); ?></label>';
companyInformation += '<input class="widefat" name="<?php echo $this->get_field_name( 'company_names' ); ?>[]" type="text" value="">';
companyInformation += '<label><?php echo __( 'Contact Email:' ); ?></label>';
companyInformation += '<input class="widefat" name="<?php echo $this->get_field_name( 'contact_emails' ); ?>[]" type="email" value="">';
companyInformation += '</div>';
container.append(companyInformation);
});
});
</script>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = sanitize_text_field( $new_instance['title'] );
// Sanitize company names and contact emails.
$company_names = isset( $new_instance['company_names'] ) ? $new_instance['company_names'] : array();
$contact_emails = isset( $new_instance['contact_emails'] ) ? $new_instance['contact_emails'] : array();
$sanitized_company_names = array();
$sanitized_contact_emails = array();
foreach ( $company_names as $index => $company_name ) {
$sanitized_company_names[ $index ] = sanitize_text_field( $company_name );
$sanitized_contact_emails[ $index ] = sanitize_email( $contact_emails[ $index ] );
}
$instance['company_names'] = $sanitized_company_names;
$instance['contact_emails'] = $sanitized_contact_emails;
return $instance;
}
} // class ContactInformation_Widget
// register ContactInformation_Widget widget
function contactinformation_foo_widget() {
register_widget( 'ContactInformation_Widget' );
}
add_action( 'widgets_init', 'contactinformation_foo_widget' );
/* Create Widget End Here */
1条答案
按热度按时间cyvaqqii1#
您的“表单”函数代码有问题。您缺少按钮选择器。只需替换这行代码,
用这个