programing

워드프레스, 게시물 유형 목록 페이지에 사용자 지정 버튼 추가

nicescript 2023. 2. 15. 23:01
반응형

워드프레스, 게시물 유형 목록 페이지에 사용자 지정 버튼 추가

이 이미지처럼 포스트 타입 페이지 위에 커스텀 버튼을 추가하려고 합니다.여기에 이미지 설명 입력

거기에 커스텀 버튼을 추가할 수 있는 필터나 액션이 있나요?

감사해요.

나는 그것을 끝낼 방법을 찾았지만 이 절차가 매우 만족스럽지 않다.더 좋은 방법이 있으면 답변을 추가해 주세요.근데 이게 도움이 될 수도 있어

add_action('admin_head-edit.php','addCustomImportButton');

이것은 편집 페이지에서만 필요하기 때문에admin_head-edit.php액션을 사용할 수 있습니다.admin_head또는 기타 (특정 요건은 아님)

/**
 * Adds "Import" button on module list page
 */
public function addCustomImportButton()
{
    global $current_screen;

    // Not our post type, exit earlier
    // You can remove this if condition if you don't have any specific post type to restrict to. 
    if ('module' != $current_screen->post_type) {
        return;
    }

    ?>
        <script type="text/javascript">
            jQuery(document).ready( function($)
            {
                jQuery(jQuery(".wrap h2")[0]).append("<a  id='doc_popup' class='add-new-h2'>Import</a>");
            });
        </script>
    <?php
}

WordPress 코어 코드를 살펴보면 해당 버튼의 후크나 필터를 찾을 수 없습니다.또한 281행에서 288행까지의 코드를 확인할 수 있습니다.단, 이 필터에 따라 버튼을 추가할 수 있습니다.

add_filter('views_edit-post','my_filter');
add_filter('views_edit-page','my_filter');

function my_filter($views){
    $views['import'] = '<a href="#" class="primary">Import</a>';
    return $views;
}

도움이 되길 바랍니다.

클래스 WP_Lists_table을 사용하는 경우(또한 사용해야 함)는 다음과 같습니다.

add_action('manage_posts_extra_tablenav', 'add_extra_button');
function add_extra_button($where)
{
    global $post_type_object;
    if ($post_type_object->name === 'shop_order') {
        // Do something
    }
}

슬프게도 여전히 유일하게 통용되는 대답이다.

그러나 응답 후 admin 헤더가 변경되었기 때문에 올바른 스크립트는 다음과 같습니다.

jQuery(jQuery(".wrap .page-title-action")[0]).after('<a href="#" class="page-title-action">Import</a>');

안타깝게도 "신규 추가" 버튼을 표시한 후 호출되는 후크가 없습니다.javascript를 사용하지 않고 추가할 수 있는 가장 가까운 위치는 제목 아래 "신규 추가"입니다.

여기에 이미지 설명 입력

이 예에서는 "edit_form_top" 후크를 사용하여 커스텀 투고 유형 "Event"에 버튼을 추가했습니다.

add_action('edit_form_top', 'add_custom_button');

function add_custom_button($id){
    if ($post->post_type != 'event') return false;
    echo('<button>Custom button</button>');
}

Touqeer Shafi의 답변은 저를 올바른 방향으로 이끌었습니다. 저는 커스텀 포스트 타입(책)을 위해 테이블 뷰 상단의 버튼을 추가해야 했습니다.저는 포스트 부분만 변경하면 되었습니다.views_edit-post동작시키기 위해서:

add_action('views_edit-book', function($id) {
      echo('<a href="/post-new.php?post_type=book" class="page-title-action">Another new book</a>');
    });

2022년에는 WP 5.9.1과 함께 Geza Gog와 Tameroski의 답변을 조합하여 매우 효과적입니다.

add_action('edit_form_top', 'my_import_button');
function my_import_button(){
    ?>
    <script type="text/javascript">
        jQuery(document).ready( function($)
        {
            jQuery(jQuery(".wrap .page-title-action")[0]).after('<a href="#" class="page-title-action">Import</a>');
        });
    </script>
    <?php
}

언급URL : https://stackoverflow.com/questions/29811433/wordpress-add-custom-button-on-post-type-list-page

반응형