最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - How to make a stand-alone button to the post edit view

programmeradmin3浏览0评论

I'm looking to add an option to my pages that will allow the user to send out notifications that the post has been added to a subscribe list when they click the button.

I want the button to be on the side of the post edit page.

This page looks like exactly what I want, but it doesn't show how to add my code and make my code executed when the button is pressed.

Here's what I have:

function rt_custom_button(){
        $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
        $html .= '<div id="publishing-action">';
        $html .= '<input type="submit" accesskey="p" tabindex="5" value="send media note" class="button-primary" id="custom" name="">';
        $html .= '</div>';
        $html .= '</div>';
        echo $html;
}
add_action( 'post_submitbox_misc_actions', 'rt_custom_button' );

I put this is a plugin file and the button shows up. When I click it, now it publishes the post if it's not published. But I don't know how to get it to execute this code instead:

function my_custom_email() {

    $to        = 'myuserlist';
    $subject   = 'new post';
    $message   = 'title of post';
    $headers[] = 'From: ' . get_option( 'blogname' ) . ' <[email protected]>';

    add_filter(
        'wp_mail_content_type',
        'my_custom_email_content_type'
    );
    $wp_mail_return = wp_mail(
        $to,
        $subject,
        $message,
        $headers,
        $attachments
    );
    if( $wp_mail_return ) {
        echo 'Mail sent';
    } else {
        echo 'Failed';
    }
    remove_filter(
        'wp_mail_content_type',
        'my_custom_email_content_type'
);

Also, when a user clicks the button it will send email to the user list with the post and featured image if possible as an attachment.

Or if I need to add this to it's own metabox if that would be better.

I'm looking to add an option to my pages that will allow the user to send out notifications that the post has been added to a subscribe list when they click the button.

I want the button to be on the side of the post edit page.

This page looks like exactly what I want, but it doesn't show how to add my code and make my code executed when the button is pressed.

Here's what I have:

function rt_custom_button(){
        $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
        $html .= '<div id="publishing-action">';
        $html .= '<input type="submit" accesskey="p" tabindex="5" value="send media note" class="button-primary" id="custom" name="">';
        $html .= '</div>';
        $html .= '</div>';
        echo $html;
}
add_action( 'post_submitbox_misc_actions', 'rt_custom_button' );

I put this is a plugin file and the button shows up. When I click it, now it publishes the post if it's not published. But I don't know how to get it to execute this code instead:

function my_custom_email() {

    $to        = 'myuserlist';
    $subject   = 'new post';
    $message   = 'title of post';
    $headers[] = 'From: ' . get_option( 'blogname' ) . ' <[email protected]>';

    add_filter(
        'wp_mail_content_type',
        'my_custom_email_content_type'
    );
    $wp_mail_return = wp_mail(
        $to,
        $subject,
        $message,
        $headers,
        $attachments
    );
    if( $wp_mail_return ) {
        echo 'Mail sent';
    } else {
        echo 'Failed';
    }
    remove_filter(
        'wp_mail_content_type',
        'my_custom_email_content_type'
);

Also, when a user clicks the button it will send email to the user list with the post and featured image if possible as an attachment.

Or if I need to add this to it's own metabox if that would be better.

Share Improve this question edited Apr 14, 2019 at 19:49 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Sep 9, 2016 at 23:04 rudtekrudtek 6,3835 gold badges30 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

First of all you need prevent whole post form sending. Try:

function rt_custom_button(){
    $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
    $html .= '<div id="publishing-action">';
    $html .= '<button onclick="send_note(event);" accesskey="p" tabindex="5" class="button-primary" id="custom" name="">send media note</button>';
    $html .= '</div>';
    $html .= '</div>';
    echo $html;
 }
 add_action( 'post_submitbox_misc_actions', 'rt_custom_button' );

Then you need to add JS function somewhere:

function send_note(event){
    event.preventDefault();

    $.post(ajax_url, {
        action: 'send_note'
    }, function(response){
       ...
    });

}
发布评论

评论列表(0)

  1. 暂无评论