jeudi 26 mai 2016

Wordpress - How to trigger all routine functions and hooks after creating a new post from customized function

I am very new (actually my 1st attempt) to wordpress plugin coding part. From tutorials available on net, I created my custom function to create a new post (in this case it's also new post type - "question", which has been already made available by AnsPress plugin).

Relevant part of my function is like -

<?php
function AddQuestionToAnsPress($question_title, $question_content, $answer_content, $subject_id, $exam_id) {
    $path = $_SERVER['DOCUMENT_ROOT'];
    $path .= "/wp-blog-header.php";
    require($path);
    $question_title = strip_tags($question_title);
    $category_id = $subject_id;
    $category = array($category_id);
    $tag_subject_id = $subject_id;
    $tags = array(25, $tag_subject_id);
    wp_set_current_user(26);
    $user_id_for_question = 26;
    $question_array = array(
        'post_title' => $question_title,
        'post_author' => $user_id_for_question,
        'post_content' => $question_content,
        'post_type' => 'question',
        'post_status' => 'publish',
        'comment_status' => 'open'
    );
    $question_post_id = wp_insert_post($question_array);
    $question_permalink = get_permalink($question_post_id);
    wp_set_post_terms($question_post_id, $category, 'question_category');
    wp_set_post_terms($question_post_id, $tags, 'question_tag');

    $anspress_link = $question_permalink;
    return array('anspress_link' => $anspress_link, 'question_post_id' => $question_post_id);
}
?>

At present, this function is not added as plugin or in theme. Instead I am using it from a separate PHP file. This function does the primary work for me, i.e. to create a new post with post type "question". But, the problem is, my function is unable to trigger any other plugin (and their relevant activities) which are triggered after creating a new post.

For e.g. I have installed NextScript SNAP plugin in my wordpress blog, which autopost, to my facebook page, the newly created posts (but only when the post is created from wordpress itself).

So, how should I modify my function or what should I add to my function, so that it can auto-detect all the fucntions/hooks/filters which should be triggered soon after creating new post?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire