• Resolved herculesnetwork

    (@herculesnetwork)


    how to write the output of this function in field ‘tags’ on post-edit the dasboard the worpress?

    $input = array("neo", "Morpheus", "Trinity", "Cypher", "Tank");
    $rand_keys = array_rand($input, 2);
    $mixnames = $input[$rand_keys[1]];

    Thanks to all.

Viewing 15 replies - 1 through 15 (of 157 total)
  • Thread Starter herculesnetwork

    (@herculesnetwork)

    I would like to write the tags, titles, and content the same output, just missing it so I finish something I’m doing πŸ™‚

    Joey

    (@leglesslizard)

    I’m sorry I don’t understand your question. It appears here you are selecting 2 random elements from an array and then displaying the second? I think you may just want to do this:

    function return_random() {
        $input     = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
        return $input[array_rand( $input )];
    }

    I wrapped it in a function as well so if you get the data returned to you when using it. I don’t understand what you wanted to do with it, however. If you could be more clear I will try to help πŸ™‚

    Regards

    Thread Starter herculesnetwork

    (@herculesnetwork)

    hello joey, thanks for answering here too, you have helped me.
    Sorry not have been clear.
    I have a function, and want to write the output of it in normal wordpress fields, title, tags, content, the three fields that write when we create a new post, I want to fill them with the output of a function I have, it is:

    function gen_num()
    {
      global $post;
      $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
    
      if ( '' == $mixnames ) {
        $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
        $rand_keys = array_rand($input, 2);
        $mixnames = $input[$rand_keys[1]];
        update_post_meta( $post->ID, 'fieldnames', $mixnames );
      }
      // DISPLAYS THE OUTCOME
      print $mixnames;
    }

    I enter it in the post on the_content field, and I also want to enter tags in the field (meta tag posts), and title, how do? I’ve tried to start finding something in wordpress structure that suggests it is on the meta tag field, but I see nothing! I want to fill that output in wordpress fields in post-edit the dashboard, I want to see the values ​​in the field tags, title, and body of the post. You may consider a completely different approach. Use an action like ‘wp_insert_post_data’ to enter your content in normal contents of posts, shortly before being saved to the DB. because the values ​​that this function inserted into the body of the post, in the_content not appear on the instrument panel in the posts edit mode.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    for I am sending the output only for the_content, but I am sending to the post appears in the posts published on the site, but the content does not appear in the post-edit mode in the dash-board, it disrupts the issues, because I’m sending direct to the DB , and not for the original field posts. and can only with the above function, write the content, and not in title tags and filds

    Thread Starter herculesnetwork

    (@herculesnetwork)

    your solution above not write the posts, only displays the script running, I want to write, and the above function already written, but not write in dashoboard fields in post edit understand?

    Thread Starter herculesnetwork

    (@herculesnetwork)

    ‘post_content’,
    ‘post_title’,
    ‘tags_input’
    would these fields in post.php?

    Moderator bcworkz

    (@bcworkz)

    I initially said ‘wp_insert_post_data’ was an action, but it’s actually a filter, sorry for any confusion.

    You can almost just switch ‘the_content’ with ‘wp_post_insert_data’ in your earlier code that had the correct output, except with a few modifications. 1) collect $data from the filter as a function parameter. 2) instead of print statement, do $data['post_content'] = $mixnames . $data['post_content']; You can do something similar with ‘post_title’. 3) Move this last line(s) to inside the if conditional. 4) add return $data; to the end of your callback function.

    You still want to store something in post meta so new content is not added every time this filter fires.

    ‘post_content’ and ‘post_title’ are part of the data array and are stored in the posts table. Tags are handled very differently, the post ID and tag term ID are stored in term_relationships table. The ‘tags_input’ is not in the $data array, though it’s available as a second filter parameter. You cannot alter it with the ‘wp_post_insert_data’ filter, only see what it is. Altering or creating tags would need to be done in a different action/filter.

    ‘save_post’ action (not filter πŸ™‚ ) could be used to save tags using wp_set_post_tags(). For that matter, you could add content to the saved post content from this action as well. If the data added to content needs to be coordinated with that added to tags, doing it all from one callback eliminates the variable scope issue. But using this action to do so is more involved, if you are not careful you will invoke an infinite loop situation.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    // herculesnetwork content input

    function descgen()
    {
      global $data;  // befofe $post
      $mixnames = get_post_meta( $data->ID, 'fieldnames', true );
     //befofe $post->ID, this function parameter instead $data?
    
      if ( '' == $mixnames ) {
        $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ","the flash ","superman ","wolverine ", "subzero ","scorpion ");
        $rand_keys = array_rand($input, 5);
        $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]]
    .$input[$rand_keys[3]].$input[$rand_keys[4]].$input[$rand_keys[5]];
        update_post_meta( $data->ID, 'fieldnames', $data );
    
                             }
      // DISPLAYS THE OUTCOME
         if
      $data['post_content'] = $mixnames . $data['post_content'];
      $data['post_title'] = $mixnames . $data['post_title'];
         {
        return $data;
         }
    
    }

    // insert content no post, this the_content swift by wp_post_ins…

    function add_post_content($content) {
    desgen();
    return $content;
    }
    add_filter('wp_post_insert_data', 'add_post_content');

    //before ‘the_content’

    I have tried unsuccessfully to thank his care to explain clearly, but I’m very bad code skills. πŸ™

    let the tags_input out of the case, to prevent so many more questions for you, but would like to enter these values ​​in the fields post_title and post_content when you have time, please write as you would.
    thanks for your help.

    Thread Starter herculesnetwork

    (@herculesnetwork)

    note: the cleaner function without so my foolish attempts is here:

    function gen_num()
    {
      global $post;
      $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
    
      if ( '' == $mixnames ) {
        $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ","the flash ","superman ","wolverine ", "subzero ","scorpion ");
        $rand_keys = array_rand($input, 5);
        $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]]
    .$input[$rand_keys[3]].$input[$rand_keys[4]].$input[$rand_keys[5]];
        update_post_meta( $post->ID, 'fieldnames', $mixnames );
      }
      // DISPLAYS THE OUTCOME
      print $mixnames;
    }

    I was not able to apply, although we understand that you were clear, I am very noob to do so. excuse me by work.
    thanks again, you have helped me a lot.

    Moderator bcworkz

    (@bcworkz)

    Everyone was a noob once, there is no shame. At least you tried, so many do not even do that. I was not clear enough, you did exactly as I suggested, but your attempt was not quite my intent πŸ™‚

    In writing my intended code I noticed a tragic flaw. The ‘wp_insert_post_data’ does not pass the post ID so there is no good way to tell if the key words have already been inserted or not. To get the post ID we need to use an action that fires after the initial post has been saved. ‘save_post’ can work, but it can set up an infinite loop when we re-save the post, which calls our callback again, which re-saves the post, which calls our callback again…

    There is a better way. After the post is saved, transition_post_status() is called, which does several variable action hooks like "{$old_status}_to_{$new_status}". One possible hook is ‘draft_to_publish’ that only fires when the post is first published and never again for that particular post. Perfect! It saves us from needing to keep track of whether the keywords have been added earlier or not. I don’t know why I didn’t think of this earlier. Here is the result:

    function insert_gen_num( $post )
    {
      $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ", "the flash ","superman ","wolverine ", "subzero ","scorpion ");
      $rand_keys = array_rand($input, 5);
      $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]].$input[$rand_keys[4]];
      $post->post_content = $mixnames . $post->post_content;
      $post->post_title = $mixnames . $post->post_title;
      wp_update_post( $post );
    }
    add_action('draft_to_publish', 'insert_gen_num');

    Note that there is no $rand_keys[5]. Five keys are generated, the index numbers 0-4 account for all five keys: 0 1 2 3 4

    Thread Starter herculesnetwork

    (@herculesnetwork)

    Thank you again for your help and for explanations πŸ™‚
    I have tried the code above, but does not write the posts or titles in yet,
    I have a tests server on a free hosting just to make tests, that is where to trying these scripts, I would like to pass the login wordpress admin for testing, after resolved, you just copy and past here the solution, I have followed you on twitter to be able to write for private, fallow me also to activate private conversation that you send login wordpress, also I wrote a message for you on robert video. πŸ™‚

    Thread Starter herculesnetwork

    (@herculesnetwork)

    of course if you so wish, if you will spend some change here, for me it is great. As you wish πŸ™‚

    Thread Starter herculesnetwork

    (@herculesnetwork)

    I had forgotten to write the function call in single.php with:
    <? php if (function_exists ( ‘insert_gen_num’)) {insert_gen_num ();}?>

    but the values ​​change with each refresh, lack that condition to not re-write the values ​​change with each refresh (F5), lack that condition to not re-write the values ​​to each page access πŸ™‚ is almost perfect, because the values ​​that I add manually are unchanged πŸ™‚ and when access the post in edit mode the function data are really in great πŸ™‚ post your idea πŸ™‚
    in dashboast F5 not rewrite values :)but in this new case, as it would be for the values ​​written by the function written stay even after refresh (F5) in the pages?

    Thanks so much πŸ™‚

    Thread Starter herculesnetwork

    (@herculesnetwork)

    I do not know that condition that preserve the values ​​in the fields, they are changing every refresh the pages, but the dashboard does not change, and I can add values ​​as well, which allows me to do SEO πŸ™‚ and when I click on post view, is still ok on the page views on the site πŸ™‚ but a refresh and everything is changed to 5 random names, and each new refresh again changes, and when I enter the edit mode, it changes again :(, then right back to the edit mode, I do the SEO again and can press F5 at ease that the data is not changed, only after the second access of the page, or other F5 refresh.

    my attempt to keep the values ​​after a second access the pages:

    function insert_gen_num( $post )
    {
    if ( '' == $mixnames )
      $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ", "the flash ","superman ","wolverine ", "subzero ","scorpion ");
      $rand_keys = array_rand($input, 5);
      $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]].$input[$rand_keys[4]];
      $post->post_content = $mixnames . $post->post_content;
      $post->post_title = $mixnames . $post->post_title;
                           {
      wp_update_post( $post );
                           }  
    
    }
    add_action('draft_to_publish', 'insert_gen_num');
    Thread Starter herculesnetwork

    (@herculesnetwork)

    / * This function appears in the post editing and website so, but not permance the same writing, which prevents the use * /

    function insert_gen_num( $post )
    {
      $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ", "the flash ","superman ","wolverine ", "subzero ","scorpion ");
      $rand_keys = array_rand($input, 5);
      $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]].$input[$rand_keys[4]];
      $post->post_content = $mixnames . $post->post_content;
      $post->post_title = $mixnames . $post->post_title;
      wp_update_post( $post );
    }
    add_action('draft_to_publish', 'insert_gen_num');

    / * This function write and remains written but does not appear in the post fields in edit mode, which is very bad for me πŸ™ * /

    function insert_gen_num()
    {
      global $post;
      $mixnames = get_post_meta( $post->ID, 'fieldnames', true );
    
      if ( '' == $mixnames ) {
        $input = array("Neo ", "Morpheus ", "Trinity ", "Cypher ", "Tank ","the flash ","superman ","wolverine ", "subzero ","scorpion ");
        $rand_keys = array_rand($input, 5);
        $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]]
    .$input[$rand_keys[3]].$input[$rand_keys[4]].$input[$rand_keys[5]];
        update_post_meta( $post->ID, 'fieldnames', $mixnames );
                             }
      // DISPLAYS THE OUTCOME
      print $mixnames;
    }

    Another interesting note is that all script only runs when I click refresh (F5), the first access the values ​​appear not yet! I would have to go into every page and press F5 !, being a very high page number, I would have to go entering 2000 pages and exit by pressing F5 to run the code!
    Thank you so much.

Viewing 15 replies - 1 through 15 (of 157 total)
  • The topic ‘how to write the output of this function in field 'tags' on post-edit’ is closed to new replies.