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:

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

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 :)


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>

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.

| improve this answer | |
  • wow, that was fast :) it's almost perfect. Do you think it would be possible to add a dynamic class or id to the containing div so it's possible to control it via css? – molokom Dec 18 '13 at 12:19
  • @molokom in widget function see echo '<div class="my-link-box">'; there you can put all the classes you need, even based on $page, if you want. – gmazzap Dec 18 '13 at 12:37
  • wonderful. I used id="link-to-'. $page->ID .'". thanks again! One last thing: do you think it's possible to add a title to the widget just to make it easy to manage in the backend? – molokom Dec 18 '13 at 12:48
  • @molokom that can be done in every custom widget. I've updated answer code. – gmazzap Dec 18 '13 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.