Best way to add internal link in widget

I’m looking for a way to transform a static piece of code into a widget to display in different sidebars containing a simple internal link.

The basic code is:

Read More
<div class="my-link-box">
<a href="<?php echo get_page_link(90); ?>">My link text</a>
</div>

The main focus is to make it future proof so no matter the site url it should be working without a search and replace or other manual intervention.

I would like to avoid:

  • php in widget
  • hardcoded link

Maybe a widget specific shortcode?
Any help is much appreciated 🙂

Related posts

1 comment

  1. The page ID can vary in different installation, and it can’t be changed, so an option is to use get_page_by_path because the page slug can be easily changed:

    <div class="my-link-box">
    <?php $page = get_page_by_path('my-page'); ?>
    <a href="<?php echo get_permalink($page); ?>"><?php echo $page->post_title; ?></a>
    </div>
    

    However this is not a great solution as well.

    Once you are writing a custom widget, you can setup the option to select the page whose to show the link. e.g.:

    class MyPageLinkWidget extends WP_Widget {
    
      function __construct(){
        parent::__construct( false, 'Page Link Widget' );
      }
    
      function widget( $args, $instance ){
        if ( ! isset($instance['wpage']) || (int) $instance['wpage'] <= 0 ) return;
        $page = get_post( $instance['wpage'] );
        echo $args['before_widget'];
        echo '<div class="my-link-box">';
        if ( isset($instance['title']) ) {
           $title = apply_filters( 'widget_title', $instance['title'] );
           if ( $title ) echo $args['before_title'] . $title . $args['after_title'];
        }
        echo '<a href="' . get_permalink( $page ) .'">' . $page->post_title . '</a>';
        echo '</div>';
        echo $args['after_widget'];
      }
    
      function update( $new_instance, $old_instance ){
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        if ( isset( $new_instance['wpage'] ) && (int) $new_instance['wpage'] > 0 ) {
          $instance['wpage'] = $new_instance['wpage'];
        }
        return $instance;
      }
    
      function form( $instance ){
        $default = array( 'wpage'=>'-1', 'title' => '' );
        $instance = wp_parse_args( (array) $instance, $default );
        $args = array(
          'name' => $this->get_field_name('wpage'),
          'show_option_none' => 'None',
          'option_none_value' => '-1',
          'selected' => $instance['wpage']
        );
        echo '<p><label>Title:</label>';
        echo '<input class="widefat" name="' . $this->get_field_name('title') . '" type="text" value="' . $instance['title'] . '" /></p>';
        wp_dropdown_pages( $args );
      }
    
    }
    

    However, if your widget contain only links to page(s), consider to create a custom menu and use the “Custom Menu” widget.

Comments are closed.