Good day. I have a WordPress plugin script here that as below works fine in doing what it should. What I am having trouble with is editing/appending the script to do one additional task that I need. What the plugin does is setup a category in WordPress and then every hour go get a video from Youtube and create a post on the blog.
So for example, if I typed in create category: Siberian Husky's then that category would be created and every hour the script will go get a new video title, video embed and description related to Siberian Husky's.
(How the title is created now with the script ->) Let's say the first video it get's the title on Youtube is this: White Siberian Husky's Are Loveable
That would be the title to the WordPress post. What I want to do is place a text file on in my sites root directory called /phrases.txt and on each line would be a new phrase. I want this script to go get randomly a new phrase from that text file and put it "in front" of the Youtube title.
(How I want the output to be after getting a random line from my text file ->) So when the post is made on the blog the title would be: Here Is My Phrase From Text File : White Sibeian Husky's Are Loveable
I hope I made that clear enough. Below is the entire script that get's the data for the video post on the blog.
I believe around line 1861 is where the script writes the title, don't quote me, I am not positive. That part of the code looks like:
// and create a post
$wpvt_post = array(
'post_title' => $content->items[0]->snippet->title,
'post_content' => $post_content,
'post_author' => $item['item']['post_author'],
'post_status' => $item['item']['post_status'],
);
Where the ->title, is creates the title for the post. I am not sure what to change or add to have the script go to my text file and get my phrase I want to add to the title.
Here is a link to the entire script --> http://textuploader.com/kg8m
It was to large to paste into this post, sorry.
Thank you for your help.
i had not added in wordpress code here but still in PHP code it's working fine for me you had just add to this code in wordpress format. As per wordpress query and all those things.
code is here.
<?php
$f_contents = file("random.txt");
$line = $f_contents[rand(0, count($f_contents) - 1)];
?>
<?php
/*-----------------------------------------------------------------------------
Pull out a random quote from the quote file and display it.
-----------------------------------------------------------------------------*/
function random_phrase ()
{
$quotes = file ("wp-content/quotes.txt");
$num = rand (0, intval (count ($quotes) / 3)) * 3;
echo $quotes[$num] . "<br>" . $quotes[$num + 1];
}
?>
Now just put this little bit of code where you want the quote to appear:
<?php random_phrase (); ?>
Related
I have a news website with over 3000 posts. The previous website builder did not use the built-in functionality of Wordpress for the featured image, but used an ACF (Pro) image field instead.
On the new website I would like to use the standard function of Wordpress. I just have no idea how to set the images from the ACF (Pro) image field as a featured image via the built-in Wordpress function.
Is there a script that can do that automatically?
I don't like having to do more than 3000 messages manually.
So the image should be from this:
To this:
I've searched all over the internet for a solution to this, but couldn't find anything.
Thank you very much in advance for any help.
We can make a simple script to fix your post thumbnails.
For easiest implementation lets just add this function to your functions.php which will run when we your site loads.
You want to run this on over 3000 news posts. This means your site will take a few moments, maybe minutes, to run the script before your site eventually loads.
If you are using this on a live environment, lets add a URL parameter to only allow this to run when param is true, for example...
https://www.example.com/?fix_post_thumbs=true
Here is the code you need to add to your functions functions.php...
Please read comments in code so you know what is happening.
// fix post thumbs
function fix_post_thumbs() {
// if current user is admin and url param fix post thumbs is set
if(current_user_can('administrator') && isset($_REQUEST['fix_post_thumbs'])) {
// if url param for fix post thumbs is true
if($_REQUEST['fix_post_thumbs'] === 'true') {
// our wp query args for which we want to run this script
// change post type value to your news post type
$args = array (
'post_type' => 'post',
'post_status' => 'any',
'posts_per_page' => -1
);
// count the process posts
$count = 0;
// set our wp query
$query = new WP_Query($args);
// if we have posts to loop
if($query->have_posts()):
// loop through our query post results
while ($query->have_posts()): $query->the_post();
// get our acf featured image field attachment id
// 3rd parma must be false just so it returns id
// you must change 'acf_featured_img' field name to your acf image field name
$attachment_id = get_field('acf_featured_img', $query->post->ID, false);
// if there is an acf featured attachment id set
if($attachment_id) {
// set the post thumbnail with acf featured image attachment id
set_post_thumbnail($query->post, $attachment_id);
// count this process
$count++;
}
endwhile;
// output message showing count of featured images set
echo '<pre>' . print_r($count . ' featured images have been set.', true) . '</pre>';
else :
// no posts found message
echo '<pre>' . print_r('Sorry, no posts matched your criteria.', true) . '</pre>';
endif;
}
}
// finally return
return false;
}
// run our fix post thumbnails
fix_post_thumbs();
This is not tested, you may want to test on some specific news post ids first via WP_Query, using post__in to only run this on selected posts. 👍🏼
Update: I've added a print_r() information showing count of how many featured images have been set once process has completed.
Plugin Update: As suggested I've created a simple plugin which you access via the Tools menu when plugin is activated.
Simply select the post type you wish to run this function on, then select the ACF image field name you want to set as the post_thumbnail, and click run.
If ACF image field for current post has no attachment value, the function will skip this post and continue processing.
See git repo plugin link below...
https://github.com/joshmoto/acf-image-set-post-thumbnail
Or download this distribution version below to install via uploading zip file to your plugins...
acf-image-set-post-thumbnail.zip
I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).
To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).
I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.
My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.
My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??
I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!
The frame of my code is attached.
add_shortcode('sut_form', 'sut_form_shortcode');
function sut_form_shortcode() {
if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {
// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);
// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED
wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST
// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}
Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.
The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:
$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");
It is not too neat but should work.
I just inherited a custom plugin that takes Formstack submissions and creates WordPress posts from them. The posts are created fine, however the form contents are stored as serialized data in post_content.
I am tasked with enabling these posts to be edited within the WP Dashboard. Currently when you click on a post title, you are presented with a page that just shows the data; no capability to edit the data.
Enabling the editor controls within "supports" in the functions.php file gives me the editor with the serialized data just dumped in the editor.
I have never had to setup a custom edit page for a specific post type in WP. Is there someone out there who can direct me so a site that explains this? I'm running in circles.
You can filter the content before it is presented in the admin editing screen.
function my_filter_function_name( $content, $post_id ) {
if(get_post_type($post_id) == 'the_post_type_in_question'){
$serialized_content = $content;
$content_array = unserialize($serialized_content);
// do something with this array to put it in the format you want
// .....
$content = $new_formatted_content;
}
return $content;
}
add_filter( 'content_edit_pre', 'my_filter_function_name', 10, 2 );
But, that doesn't seem like it's going to be of much use to you.
In your situation, I suggest you take the time to write a script to convert all those posts so that everything is stored as post meta. (create the custom fields, first).
If your theme isn't built on any framework, then I think the quickest way to create a custom field is to use the Advanced Custom Fields plugin.
Then, once you know the meta_keys, you can write that script. E.g.
$posts = get_posts('post_type'=>'the_post_type','posts_per_page'=> -1);
foreach($posts as $post){
$content_array = unserialize($post->post_content);
// how you do the next bit will depend on whether or not this is an associative array. I'm going to assume it is (because it's a little easier :) )
foreach($content_array as $meta_key=>$meta_value){
update_post_meta($post->ID, $meta_key, $meta_value);
}
// just put what you actually want as the post content back into the post content:
wp_update_post(array('ID'=>$post->ID,'post_content'=>$content_array['post_content'])); // assuming the key of the element you want to be the post content is 'post_content'
}
To run this script, you could simply create a temporary new page and then create a template file specifically for that page and put the above code into that file (then visit the page).
You need to modify the plugin so that the data is unserialized before modifying, then serialized before saving to DB...
Alternatively, try using WP's CORE functionality:
https://codex.wordpress.org/Function_Reference/maybe_serialize
https://codex.wordpress.org/Function_Reference/maybe_unserialize
https://codex.wordpress.org/Function_Reference/is_serialized
https://codex.wordpress.org/Function_Reference/is_serialized_string
Can anyone demonstrate a basic example that adds a link on an arbitrary page in the pdf that takes the user back to the table of contents?
I have everything working fine, but can't get this simple requirement going.
Some background:
You'd think this would be trivial, everything about TCPDF seems to function so well, maybe a bad day I'm having. I have tried the following:
From the documentation there seems to be 2 options for creating internal document links
Via methods addlink() and setlink() to specify a target page.
Via an html anchor tag rendered in the pdf (where you specify the href attribute as #15 for page 15 for eg.)
These methods basically work just fine.
A quote from the author -
The Table Of Content page is created on the current page and then moved to the specified page...To create a link to the TOC page you have to set a link to the last page (you must know the total number of pages before).
I Understand this, I create the TOC last (using the supplied methods), and make a link to this last page, but it is unclickable (not a link in the rendered doc). I therefore have to interpret the quote 'you must know the total number of pages before' as meaning TCPDF must know the number of pages! Clearly a big difference, meaning for most practical purposes the answer is no, not by this method (maybe one link on the last page!)
Finally, the documentation for the addTOC method mentions the $toc_name argument:
TCPDF::addTOC (
$page = '',
$numbersfont = '',
$filler = '.',
$toc_name = 'TOC',
$style = '',
}
$toc_name (string) name to use for TOC bookmark.
Unfortunately I can't see anyway to use this name, no docs, help or examples anywhere.
Someone.. please tell me I'm being silly..
It may be late, but this is what I use (my table of contents is on page 2):
$pdf->addTOCPage();
$link = $pdf->AddLink();
$pdf->SetLink($link, 0, '*2');
$pdf->addTOC(2, 'courier', '.', 'INDEX', 'R', array(128,0,0));
$pdf->endTOCPage();
And then at where ever I want to link back to the TOC in the document, I do the following:
$html = 'Return to TOC';
$pdf->writeHTML($html, true, false, true, false, 'R');
TCPDF Bookmark:
$bookmark = "Title of my Bookmark";
$pdf->Bookmark($bookmark);
This is your best bet for adding to the PDF Bookmarks.
my question a little complicated.
I want to share a featured image which is in a post on my wordpress site to twitter.
But there a important point. i want to share a picture with twitpic because of people can see this image in twitter without open my web site.
i get api from twitpic, and i use this code (link)
but twitpic or this code cannot accept a url to upload twitpic and get a link.
$resp = $twitpic->upload(array('media'=>'$a2', 'message'=>'get_the_title()'));
if i wrote $a2 = "ex.jpg" its working,
if i wrote $a2 = "http://www.ex.com/ex.jpg" not working
then i try that;
<?php
$a1 = $_SERVER['DOCUMENT_ROOT'];
// ex image remove 18 chr from begining http://*******.com/wp-content/uploads/2013/04/2012-10-17-23.48.34.jpg
$kisalt1 = $imageshare[0]; // wordpress featured image fxn ex: http://*******.com/wp-content/uploads/2013/04/2012-10-17-23.48.34.jpg
$kisalmis = substr($kisalt1,18,9999);
$a2 = $a1.$kisalmis;
// echo $a2; => /home/*******/public_html/wp-content/uploads/2013/04/bu-terste-bir-islik-var-panpa.jpg
?>
but it stil give same error.
Unable to find or read file
how can i get ride of this?
summary => i want to this => wordpres featured image to twitpic for share twitter like image which can open without click a link.
Edit: I just tried deleting this because I saw that you said that http://www.ex.com/ex.jpg doesn't work for you. But then I thought this might still be helpful (to someone), and for some reason the comments option didn't show up for me, so here it is again. Sorry if it doesn't help though.
What you need is just the post_thumbnail url, not the full image tag.
http://wordpress.org/support/topic/getting-post-thumbnail-url
The relevant post is the one with this code block:
$a2 = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
Does it work if you add that?
-Andrew
Another edit...I just came across this post on the Wordpress StackExchange site: https://wordpress.stackexchange.com/questions/83137/post-thumbnail-relative-link-and-html-modify
Does it work if you provide TwitPic with a relative link location?