edit_post_linkにtarget指定して新規ウインドウで開く

edit_post_linkはテンプレートのloop内に書くと、表示画面で編集画面へのリンクが出る便利な関数。

最近のWordpressは、表示画面上に

こんなのが出るから、あまり必要性はないかもしれない。

本題に戻ると、edit_post_linkのtargetは同一ウインドウなので、自分としてはちょっと使いづらい。
できれば、blankで開きたいということで

// function.php
add_filter('edit_post_link', 'my_post_link');
function my_post_link($output) {
    return str_replace('<a ', '<a target="_blank" ', $output);
}

edit_post_linkのaタグをリプレースしてtarget指定を入れてみた。


ちなみに、
上に表示されるバー(AdminBar)を変更するには、

// function.php
function remove_admin_bar_links() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('edit');
}
add_action( 'wp_before_admin_bar_render', 'remove_admin_bar_links' );

function admin_bar_edit_option() {
    global $wp_admin_bar;
    if ( !is_super_admin() || !is_admin_bar_showing() )
        return;
    $current_object = get_queried_object();
    if ( !empty( $current_object->post_type ) &&
        ( $post_type_object = get_post_type_object( $current_object->post_type ) ) &&
        current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
    ) {
        $wp_admin_bar->add_menu(
            array( 'id' => 'new-edit',
                'title' => '投稿編集',
                'href' => get_edit_post_link($current_object->term_id),
                'meta' => array('target'=>'_blank')
            )
        );
    }
}
add_action( 'admin_bar_menu', 'admin_bar_edit_option', 40 );

参考:
http://varl.jp/note/admin-bar-hide-remove-add-menu
http://digwp.com/2011/04/admin-bar-tricks/
http://www.problogdesign.com/wordpress/add-useful-links-to-wordpress-admin-bar/
http://sumtips.com/2011/03/customize-wordpress-admin-bar.html

「edit_post_linkにtarget指定して新規ウインドウで開く」への1件のフィードバック

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)