0

I am trying to dynamicly add some content post (also containing some js), but the javascript part is filter out. I thought by using wp_set_current_user the js would be included.

This is my main function which generate the post

   function mydomain_which_page2($atts) {
    extract( shortcode_atts( array(
        'temp_title' => 'something'
    ), $atts ) );
        //post exists?
    if( wp_exist_post_by_title( $temp_title ) ) {
        $redirect_title = sanitize_title( $temp_title);
        header('Location:' . $redirect_title);
        exit();
    } else { 
        // post does not exist
        wp_set_current_user(NULL, 'Test user');
        //GET THE MAPS
        $output = getIncludeContent('myotherdomain.com/generate.php'); 
        //SAVE AS WP POSTS
        mydomain_add_post($temp_title,$output);
        //REDIRECT
        $redirect_title = sanitize_title( $temp_title);
        header('Location:' . $redirect_title);
        exit();
    }   
}

directly going to myotherdomain.com/generate.php will output the js in source code also $output will show the source code with js. and add_post function

function add_post($title, $content){
    // Create post object wp_strip_all_tags
    //$content = strip_tags($content);

    $my_post = array(
      'post_title'    => $title,
      'post_content'  => $content,
      'post_name'   => sanitize_title($title),
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array(1)
    );

    // Insert the post into the database
    wp_insert_post( $my_post ); 
}

Any tips, suggestions?

regards

2
  • Exactly what works and what doesn't in that code? Where does it fail?
    – s_ha_dum
    Commented Dec 1, 2013 at 16:12
  • if fails posting the javascript which is generated on my other website. I think i found a solution see my answer
    – alex
    Commented Dec 1, 2013 at 17:16

1 Answer 1

0

i think i found a solution

function add_post($title, $content){
    // Create post object wp_strip_all_tags

    if (!is_user_logged_in()){
        $user_id = 1;
        wp_set_current_user($user_id,$user_login);
        wp_set_auth_cookie($user_id);
        do_action('wp_login',$user_login);
    }

    $my_post = array(
      'post_title'    => $title,
      'post_content'  => $content,
      'post_name'   => sanitize_title($title),
      'post_status'   => 'publish',
     // 'post_author'   => 1,
      'post_category' => array(1)
    );

    // Insert the post into the database
    wp_insert_post( $my_post ); 
}
1
  • $user_login is never set to a value. You should be seeing a PHP warning when the user is not logged in. Commented Dec 2, 2013 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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