2

We're developing across two sites. One site holds our content, the other actually displays it.

So when we are viewing posts in the WP admin, the View link is pointing to the instance it's on, which doesn't help us when we want to view the post. For example,

Data site: http://site1.com

Viewing site: http://site2.com

I need to change our view link on the Data site to something like: http://site2.com/12345

Is there a hook I can use to modify what is set in the View link in the WP admin?

1 Answer 1

2

The "View" link I believe you were referring to isn't the only place the url should change so I took the liberty and changed them in the other spots as well.

This affects only published posts and pages.

View link changes

It's quite a bit of code but this should do it.

// Change the "View" link on the all posts page.
function my_custom_view_link( $actions, $page_object ) {

  $title = _draft_or_post_title();

  $actions['view'] = sprintf(
    '<a href="%s" rel="permalink" aria-label="%s">%s</a>',
    my_custom_permalink( $post->ID ),
    esc_attr( sprintf( __( 'View &#8220;%s&#8221;', 'my-text-domain' ), $title ) ),
    __( 'View', 'my-text-domain' )
  );

  return $actions;
}
add_filter( 'post_row_actions', 'my_custom_view_link', 10, 2 );


// Change the "Post updated" link on the edit post page after saved.
function my_custom_post_updated_messages( $messages ) {
  global $post_ID;

  $view_page_link_html = sprintf( ' <a href="%1$s">%2$s</a>',
    esc_url( my_custom_permalink( $post_ID ) ),
    __( 'View post', 'my-text-domain' )
  );

  $messages['post'][1] = __( 'Post updated', 'my-text-domain' ) . $view_page_link_html;

  return $messages;
}
add_filter( 'post_updated_messages', 'my_custom_post_updated_messages', 10, 1 );


// Change the "Post updated" link on the edit post/page after saved.
function my_custom_post_preview_link( $return, $post_id, $new_title, $new_slug, $post ) {

  if ( 'publish' !== $post->post_status ) {
    return $return;
  }

  if ( false === strpos( $permalink, '%postname%' ) ) {

    $view_link      = my_custom_permalink( $post_id );
    $display_link   = urldecode( $view_link );
    $preview_target = " target='wp-preview-{$post->ID}'";

    $return = '<strong>' . __( 'Permalink:', 'my-text-domain' ) . "</strong>\n";
    $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";

  }

  return $return;
}
add_filter( 'get_sample_permalink_html', 'my_custom_post_preview_link', 10, 5 );


// Replace the base url with a custom one.
function my_custom_permalink( $post_id ) {
  return str_replace( home_url(), 'http://site2.com', get_permalink( $post_id ) );
}
1
  • How would this be applied to a custom post type? Or is there a way to affect basically all post types including pages, posts, custom post types?
    – knox
    Commented Oct 25, 2022 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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