1

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' ) . ' <my@email.com>';

    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.

0

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){
       ...
    });

}
  • is this possible without js? I know nothing about it... just php? – rudtek Sep 9 '16 at 23:34
  • JS here used only to send request to php! It must be used here to stop whole post form to proccess, without it post will be published always. You also can attach mail sending on post publish if you want. So when publish button clicked mails function will be processed too. – WordPlus Hosting Sep 9 '16 at 23:46
  • i don't mind if it publishes when it's posted but want to make sure that it will send the mail. how to i call the function just by a button push and let it be okay if it ALSO publishes? – rudtek Sep 9 '16 at 23:57
  • would there be a better way to do this with another button somewhere else? – rudtek Sep 12 '16 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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