Skip to content

Instantly share code, notes, and snippets.

@krystyna93
Last active June 10, 2023 13:11
Show Gist options
  • Save krystyna93/b879b0b7ec6f158b98ff0e253cb31ce5 to your computer and use it in GitHub Desktop.
Save krystyna93/b879b0b7ec6f158b98ff0e253cb31ce5 to your computer and use it in GitHub Desktop.
Custom WordPress Simple Date Widget
<?php
class Custom_Page_Title_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'custom_page_title_widget', // Base ID
esc_html__( 'Custom Page Title Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Add a Custom Page Title to your pages', 'text_domain' ), ) // Args
);
}
/**
* 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 ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// Get the current post object
global $post;
// Output the custom page title if it exists
if ( isset( $post->ID ) && get_post_meta( $post->ID, 'custom_page_title', true ) ) {
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo '<h1>' . get_post_meta( $post->ID, 'custom_page_title', true ) . '</h1>';
echo $args['after_widget'];
} else {
return;
}
}
/**
* 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'] );
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $new_instance['nonce'] ) && wp_verify_nonce( $new_instance['nonce'], 'custom_page_title_widget' ) ) {
return $instance;
}
return;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// Get title
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
// Widget nonce
$nonce = wp_create_nonce( 'custom_page_title_widget' );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></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>
<input type="hidden" name="<?php echo $this->get_field_name( 'nonce' ); ?>" value="<?php echo esc_attr( $nonce ); ?>">
<?php
}
}
/* This code should add a new widget called Custom Page Title Widget to your WordPress site.
The widget will allow you to add a custom page title to any page on your site.
To use the widget, simply drag it into a widget area in your WordPress Dashboard, fill in the title you want to display on the widget,
save the changes, and the widget should appear on the front-end of your site.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment