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="https://wordpress.stackexchange.com/questions/126876/<?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.