how to write the output of this function in field 'tags' on post-edit
-
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.
-
BMWorkZ, returning to the previous case, I noticed that every time I clicked on draft before click publish, always work in 100% of cases, then made a auto-draft_to_publish, is now 100% working, YOUR plugin;)
If you can also give a little help to insert the value of the title in post_content;)
I will try to follow their advice with the question of post_title copier for post_content, but will be for already existing posts, missed me be clear with you really, I should have informed you need to add values ??in posts existing now too, but for new posts , also I need and your code works well for manual postings, he automatism a lot, but the method of this code not worked with a plugin that creates posts for me, but this code works here with my plugin:
add_filter( 'wp_insert_post_data' , 'add_string_on_publish' , '999', 2 ); function add_string_on_publish( $post_content, $postarr='' ) { $input = array(" Neo ", " Morpheus ", " Trinity ", " Cypher ", " Tank ", " the flash "," superman "," wolverine ", " subzero "," scorpion "); $rand_keys = array_rand($input, 2); $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]]; $post_content['post_content'] .= $mixnames; return $post_content; }
the question for new posts was remedied ?? both new textbooks posts I will use your code for speed, and for those who will use with my self-posting plugin that only this above code works together. should be a matter of order of things when your code will go, the posts are created in front, and IDs created before your code act, your plugin not work in conjunction with this plugin, but after the self-draft_to_publish his plugin is perfect for when I do a manual posts. It would be another problem for me that your code already delivered me ??
and their advice on the title are stored here and I will try quite later ??
—————–
I now need to add values ??on all poles of a certain category, I get exactly what I want, but not the old problem appears in post_content
add_filter('the_content', 'custom_category_text'); function custom_category_text($content){ global $post; $input = array(" Neo ", " Morpheus ", " Trinity ", " Cypher ", " Tank ", " the flash "," superman "," wolverine ", " subzero "," scorpion "); $rand_keys = array_rand($input, 2); $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]]; $custom_category_text = $mixnames; if(in_category('622')){ // change catetegory ID 622 $content = $content . $custom_category_text; } return $content; }
I want to add data to all posts of category 622, but I want the data appears in post_content ?? this code already works well. not only appears on the dashboard.
Thanks so much ??
Hi, BCWorkZ
well I analyzed your text (my english is bad I am Brazilian, when I live in a country that speaks native English I can fix it better and read better books of programming languages) I am curious to stare at the codes and seeing that the codes do for others and adapting to my needs, not yet started studies in programming.
And yes this is really my goal: add content to existing posts, add post when already has an ID and keys on site, so I call- data, apply to actions in so-called data. inject contents into parts that already exist contents, my approach was not correct actually making it look like I just wanted to create new posts, but even so, your solution will help me a lot, for golds cases I know I will face, missing my way to ask for help I was helped with solutions that certainly I would have to worry about the future.
??but back to the current problem, I need to call posts, and add content to them :), as my above function, and I can not add contents to all posts from a particular category, as do the action you want with it, but as always, my concern and have this data in edit mode in post_content fields.
What changes would you do in the code above?
Thanks BCworkZ.You are doing very well with English! MUCH better than my Portuguese ?? Technical communication is particularly difficult when in a non-native language.
Unfortunately, the code you propose changing is not very useful except for the part that generates the key word string to insert. When someone is learning to code it is tempting to try to piece together snippets of code to achieve their goal. This can actually be more difficult than writing your own code, but because you are not sure how to proceed it seems like the only solution.
The first thing to learn when coding a new language is the proper syntax. There are online tutorials (in English at least, maybe a few in Portuguese?) that explain this, but you will still make many mistakes. This is OK, this is how we learn. Fortunately, PHP has a decent error handling system. If you haven’t, you need to enable error messages by defining
WP_DEBUG
as true in wp-config.php.If you focus on coding very small tasks, you will have better success in achieving your goal. Once that small task works, you can add another small task to it. It will be slow going at first, but as you develop skills it goes faster. Eventually, all those small tasks added together will achieve your overall goal. Additionally, you will have learned much and fully understand what the code is doing. This does not happen when you piece together snippets.
By all means use snippets as examples of what to do. It’s a good way to learn new functions and techniques. But try to write your own code ??
I’ve outlined what to do in an earlier post, except I was thinking you intended to insert key words in all posts. To limit which posts are altered we change the arguments to
get_posts()
. You can use any argument used in the WP_Query class. You can limit posts to a category ID with'cat' => $cat_id,
as part of the argument array. If you have a huge number of posts to process, to avoid memory issues, you can limit how many posts to get at one time with ‘posts_per_page’ and ‘offset’. These arguments are also good for testing your code, limiting the posts altered to just a few.Consider using a second category to keep track of which posts have been processed. Use
wp set post terms()
to add a category. Use'category__not_in' => array( $exclude_id ),
as part of theget_posts()
arguments to avoid getting processed posts.You need some way to tell this code to start processing. One thing you could do is place all of your code on a custom page template. Create a page based on the template. Simply opening this page could be enough to start the processing. Additional safeguards could be put into place to prevent accidental running of your script. There’s all sorts of ways to do this. A particular URL parameter is one. There’s also many ways to trigger the script, loading a page is only one, it is an easy one though.
I suggest, to begin this little project, you simply set up a custom page template that gets a limited number of posts in a particular category like I described. For now, simply output the titles so you can be convinced that
get_posts()
is working correctly.Next try adding the key words into the title and updating the post. Once that is working right, adding the same for content should be easy. The final piece is adding another category so the post is not processed more than once.
If you get stuck, post what code you have here and I (or someone) can help you out. Good luck, and try to have fun too!
Hi BCWorkZ, sorry for the delay in response, but I was change of address, status changing in Brazil. a radical change and I was without internet connection. Thanks for the feedback, I will now analyze your answer. ?? Just now getting a connection. thanks again.
Hi BcWorks,
??thank you very much your clarification of texts on the case, it will certainly help a lot and have ideas to write their syntax’s own for similar cases, and also me when I start my studies in wordpress advanced and basic php, but I do not even know the basic structure a syntax php to try to write my own, I always used wordpress to use plugins, and recently had interest in learning to program and learn about the page structure of wordpres files to myself write, but the interest is recent, still not had time to initiating studies in the area, and my job is just webmaster, i’m administrator 5 websites, and I’m not taking time to study, do not understand how to start writing my code, I’m interested and already wrote some silly little things to learn video-classes, but very basic thing, still do not know to create solutions, not left me time for that at the moment, and I need to solve this problem because I have a few thousand posts to insert contents, and when you have the time to write to me as you wrote excellent solution to enter into new posts, what you have written here will serve for consultation later when I have no idea how I even write, I have a great desire to understand the code. but now not even spare me time to study. ‘I want to add values ?of a function in all posts in a particular category’ I will be very grateful if you could write.I don’t normally write code for people in these forums. People who cannot code should hire someone who does. In my own small way I wish to help coders trying to make a living at coding. I am NOT suggesting that I want to be paid here.
I don’t mind helping people learn, but simply providing a complete solution does not further one’s knowledge nearly as much as having you work things out for yourself with some assistance. I do understand time constraints, but if you started out last week by trying code your own solution instead of asking in so many different ways to get a complete solution worked out for you here, you would already have it and would have learned much along the way.
Here we are now, with little to show for all the effort expended. In consideration for the efforts you have made, I’m breaking my own rule and providing you with a complete solution in the form of a custom page template. Place the code in a new PHP file and save it into your active theme’s folder. Update the category ID in the ‘KEY_ID’ definition on line 9 as instructed by the comment on that line. Create a new page based on the “Update Keys” template. The title should serve as a warning to not view the page unless one wants to update a bunch of posts. Perhaps “Viewing this page alters many posts automatically”
The script currently only updates 2 posts at a time. View the page a few times and verify it is working exactly as you intend. When you are satisfied all is correct, change the posts per page argument (line 18) to something like 500. Too large a number will cause the script to time out before it is finished. A smaller number means you need to reload the page more times to get through all of the posts.
Once the template file is set to process a large number of posts, make a full backup of your DB before viewing the page again. While I’ve tested this script on my installation, I cannot be responsible if this script does things to your posts that you did not want. You need to be able to restore the original state.
Here’s the complete template file code. Use with great caution. The page will display “Updating posts… No posts found to process” when all posts in the category have been processed.
<?php /** * Template Name: Update Keys * Adds key words to posts. Create a new page based on this template. * WARNING! * Script changes posts automatically on page view or reload - be very careful!! */ define('CAT_ID', 622 ); // Category ID of posts to update with keys define('KEY_ID', 999 ); // Create a "has-keys" category and change 999 to the has_keys category ID get_header(); ?> Updating posts...<br> <?php $args = array( 'cat' => CAT_ID, 'category__not_in' => array( KEY_ID ), 'posts_per_page' => 2, // Maximum # posts to process on page load ); $posts = get_posts( $args ); if ( 0 == count( $posts )) wp_die("No posts found to process"); $input = array(" Neo ", " Morpheus ", " Trinity ", " Cypher ", " Tank ", " the flash "," superman "," wolverine ", " subzero "," scorpion "); foreach ( $posts as $post ) { $rand_keys = array_rand($input, 2); $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]]; $post->post_title = "$mixnames {$post->post_title}"; $post->post_content = "$mixnames {$post->post_content}"; wp_update_post( $post ); wp_set_post_categories( $post->ID, array( KEY_ID ), true ); } ?> Posts updated <?php get_footer();
When you are finished updating posts, I suggest you delete the page based on this template to prevent accidental running of the script. Keep a copy of the template in a safe place because a theme update may delete the copy in the theme folder.
I really would like to understand the codes, already watched a Brazilian coach called vipacademy on youtube that promised to teach php, I learned the basics and practiced, but he stopped in 6th 10-minute lessons each, I’ve seen courses on youtube that promise basic, intermediate and advanced, but only in English and Spanish, I do not know listen in English and Spanish not even know the basics, not find much material in our language, in Brazil there are great programmers, indicating great books, all in English, and recently I I have a job Brazilian minimum wage and spare time I am webmaster taking care of 3 websites that could never monetize without enough public to draw minimum value, and I’ve been trying to make money, to work only at home, and take time to learn program, because I know that the sky is the limit for those who program and has a great idea. looking at your code, still did not understand it, I will use it without understanding how it works, but my intention is to understand, and write something more complex and my way as you suggested. but really my ceaselessness not allow me to start writing myself yet, I write in html and css basic, which seems increasingly easy, are markup languages, simple, I started yesterday watching PHP classes again from hugo vasconcelos, watch and repeat and do yourself the steps of the class to practice understanding. I will test your code now. but it was not spent, I really want and I will learn and all that you have written, will serve for consultation for myself and others, I’m just late to correct my posts and learn how to program before that let my duplicate post to the google because yet They have weeks are posted and need these corrections, and my order correct but what is disturbing me, I will make backups before using it. Thank you again.
You’re welcome!
It is unfortunate that you are unable to find more material in Portuguese. Studying technology through foreign language videos is particularly difficult. I have found for myself that the best way to learn coding is to give yourself a simple programming assignment and work through it until it works properly. It will be slow going and frustrating, but you learn from mistakes. Make many mistakes and you learn a lot!
I recommend that such an assignment not involve WordPress, that complicates matters because you need to understand the complex WP code base. Better would be a simple PHP task that does something related to your personal interests and needs. Everyone learns differently, so you need to follow what works for you, but eventually you will need to write your own code to progress.
Best of luck to you ??
Thanks BCWORKZ, your code is a good way to do things ??
I have a problem that comes a black screen and after running the script, it does the job, but not get anything after making the first activation, gets a black screen, I have to go via ftp and to delete the folder plugin, so I can work. to another category, I reinstall the plug with another ID number category, and do the work, and again delete the folder, if not stay in black screen, I could just edit the plugin code edit the ID, but not out of black screen until I delete the plugin via FTP.
error: Fatal error: Call to undefined function is_rtl () in /opt/lampp/htdocs/wordpress/wp-includes/post-template.php on line 550 which missed to do something?? I’m sorry my template code is causing you trouble. Unfortunately, I cannot replicate the issue on my installation, so I can only guess at what may be the problem. The fact that it works, but throws an error afterwards means the only code that could be causing the error is
get_footer()
, which should never be a problem. But it is a problem for some reason.One thing to try is to deactivate the plugin that adds keys to new posts. You do not need it to add keys to existing posts. If you still have problems, try switching to a twenty* theme just to update the existing posts. Copy the template file to whatever theme you use. You can restore your normal theme once the updating is finished.
I hope one or the other of my suggestions works for you because there isn’t much else to try. The only other thing to try is deactivating all plugins and using one of the twenty* themes with the custom template file. If this last effort still yields a black screen and error, then I would suspect there is an issue with your WP installation itself. In that case try updating WP to 4.5.
hi, bcworkz, thanks again ?? The solution I found was to eliminate head and footer that really do not care about their presentation to only a beautiful screen while running, even without any screen already be very good ??
the problem is that my every move, no matter where I go on my dashboard, happens a new reload page, do not have or disconnecting the plug, because it runs again, and add more values ??to the posts of the category, it would be good other action than a load page, such as a button I think, that you understand better, but I think it would be enough a button. so I can get in and out of directories on the dashboard, it performs so many times for me to do anything I have to comment all the code via FTP to in order to see another screen in the dashboard without the plugin execution screen, any movement causes the script to run again. action to need, you did for me ?? only the movement that calls action that is not working well. when you have time you answer :).<?php /* plugin name: add content by cat 2 version: 0.1 author URI: https://myplugin.com author: myplugin description: WORKING, ADD TO ALL POSTS OF CATEGORY, AVAILABLE IN DASHBOARD, BUT ONLY A EXECUTION, */ /* define('CAT_ID', 6 ); // Category ID of posts to update with keys define('KEY_ID', 999 ); // Create a "has-keys" category and change 999 to the has_keys category ID ?> Updating posts...<br> <?php $args = array( 'cat' => CAT_ID, 'category__not_in' => array( KEY_ID ), 'posts_per_page' => 2, // Maximum # posts to process on page load ); $posts = get_posts( $args ); if ( 0 == count( $posts )) wp_die("No posts found to process"); $input = array(" Neo ", " Morpheus ", " Trinity ", " Cypher ", " Tank ", " the flash "," superman "," wolverine ", " subzero "," scorpion "); foreach ( $posts as $post ) { $rand_keys = array_rand($input, 2); $mixnames = $input[$rand_keys[0]].$input[$rand_keys[1]]; $post->post_title = "$mixnames {$post->post_title}"; $post->post_content = "$mixnames {$post->post_content}"; wp_update_post( $post ); wp_set_post_categories( $post->ID, array( KEY_ID ), true ); } ?> Posts updated
Thanks again.
I tried to comment all the code, put the id of the category you want to apply at the time, back in the dashboard, apply a single reload, return to the code and comment on it, but always the code already made 3 or 4 plays, has 3 or 4 times the desired value. I see that the only problem is to have a reload as the action call.
the executions minimum to get were two doing so, with the already plugin enabled, I uncomment all the code and go to the dashboard, and the already plugin enabled, click on disable, and this act is already a reload, and values They will bent in posts category. thanks again.Ah, I see what’s wrong now. You do NOT want to use my template code as a plugin!! As you observed, the code runs every time you do something.
The script needs to be utilized as a theme template. Please use the header comment as I provided before and place the file in your active theme’s folder. Create a new page through the admin menu, giving it a title and selecting “Update Keys” as the template. No content is needed.
This way, the script only runs when that specific page is requested. It’s possible to add an activation button, but it’s just more code. Loading the page to run the script isn’t a problem with a page template, it’s only a problem as a plugin.
yes yes, I do not have specific wishes, if I use as plugin or part of the theme, or in the functions.php, as this code is only for my own use and do not care to have beautiful page, power button, etc … It can be super simple, just do the action you did for me ?? rather use as a page’s theme, where only access it in my admin, would be perfect. his idea was good, I had not understood that. I will fix here ?? thanks again.
- The topic ‘how to write the output of this function in field 'tags' on post-edit’ is closed to new replies.