I've used wp_handle_upload from the front end and it works fine.
Now I want to receive base64 string (jpg) from API POST store in metabox then turn it in to jpg(until here it works fine). Then I need to upload it in the media library and attach it to a post.
when I pass the file with file_get_contents or fopen it does not work. Any ideas?
function base64ToImage($base64){
$img = base64_decode($base64);
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// wp_handle_upload( $_POST['img'], 0 );
$fp = fopen(get_template_directory()."/xxxxxxxxxxxxxxxxxxxxxxxxx.jpg", "w+");
// write the data in image file
fwrite($fp, base64_decode($base64));
// close an open file pointer
fclose($fp);
wp_handle_upload( file_get_contents("../" ."/xxxxxxxxxxxxxxxxxxxxxxxxx.jpg"), 0 );
return 0;
}
So to my past self and to anyone that might find this useful.
I used the wp_upload_bits function instead, subsequently I used
-wp_insert_attachment
-wp_generate_attachment_metadata
-wp_update_attachment_metadata
-set_post_thumbnail in order to associate the image with a specific parent post.
I'm getting the error WP HTTP Error: cURL error 28: Operation timed out after 1001 milliseconds with 0 bytes received using the fetch_feed() method in my Wordpress Plugin.
This is for trying to fetch a larger RSS feed and I need to increase the Curl Timeout. Not sure why it is set to 1 second instead of 5 also?
The WP Documentation on this is not very detailed WP_Feed_Cache notably that SimplePie_Cache class documentation is not present.
Any help would be appreciated, not sure if I'm able to hook into SimplePie to increase the Curl Timeout. Also, I tried rewriting my own fetch_feed() method with no success below:
public function fetchFeed( $url ) {
if( ! class_exists('\SimplePie', false) ) {
require_once( ABSPATH . WPINC . '/class-simplepie.php' );
}
require_once( ABSPATH . WPINC . '/class-wp-feed-cache.php' );
require_once( ABSPATH . WPINC . '/class-wp-feed-cache-transient.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-file.php' );
require_once( ABSPATH . WPINC . '/class-wp-simplepie-sanitize-kses.php' );
$feed = new \SimplePie();
$feed->set_sanitize_class( 'WP_SimplePie_Sanitize_KSES' );
// We must manually overwrite $feed->sanitize because SimplePie's
// constructor sets it before we have a chance to set the sanitization class
$feed->sanitize = new \WP_SimplePie_Sanitize_KSES();
/* Customize sanitization */
$feed->sanitize->enable_cache = false;
$feed->sanitize->timeout = 60;
$feed->sanitize->useragent = "Custom Testing Feed Reader";
$feed->set_cache_class( 'WP_Feed_Cache' );
$feed->set_file_class( 'WP_SimplePie_File' );
$feed->set_feed_url( $url );
$feed->set_timeout( 30 );
/** This filter is documented in wp-includes/class-wp-feed-cache-transient.php */
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 60, $url ) ); //changing cache time to 60 seconds (instead of 12 hours)
/**
* Fires just before processing the SimplePie feed object.
*
* #since 3.0.0
*
* #param object $feed SimplePie feed object (passed by reference).
* #param mixed $url URL of feed to retrieve. If an array of URLs, the feeds are merged.
*/
do_action_ref_array( 'wp_feed_options', array( &$feed, $url ) );
$feed->init();
// $feed->set_output_encoding( get_option( 'blog_charset' ) );
$feed->set_output_encoding( "UTF-8" ); //set statically to UTF-8
if ( $feed->error() )
return new \WP_Error( 'simplepie-error', $feed->error() );
return $feed;
}
I was able to increase the Curl Timeout by using the following code:
//Set HTTP Request Timeout
add_filter('http_request_args', 'my_http_request_args', 100, 1);
function my_http_request_args( $r ) {
$r['timeout'] = 30;
return $r;
}
//Setting WP HTTP API Timeout
add_action('http_api_curl', 'my_http_api_curl', 100, 1);
function my_http_api_curl( $handle ) {
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt( $handle, CURLOPT_TIMEOUT, 30 );
}
// Setting custom timeout for the HTTP request
add_filter('http_request_timeout', 'my_custom_http_request_timeout', 101 );
function my_custom_http_request_timeout( $timeLimit ) {
return 30;
}
How to use wp_extract_urls() in WordPress if I want to find NOT unique links? I just want to count all the links in my post!
Your best bet is likely to create a new function in your functions.php file that excludes the array_unique() part of the existing function. Something like this should work -
function wp_extract_all_urls( $content ) {
preg_match_all(
"#([\"']?)("
. "(?:([\w-]+:)?//?)"
. "[^\s()<>]+"
. "[.]"
. "(?:"
. "\([\w\d]+\)|"
. "(?:"
. "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
. "(?:[:]\d+)?/?"
. ")+"
. ")"
. ")\\1#",
$content,
$post_links
);
$post_links = array_map( 'html_entity_decode', $post_links[2] );
return array_values( $post_links );
}
I have a function working for wordpress front-end forms except for one thing. I can upload images from the front-end form but if I opt NOT to add a file, I get the following error:
Object of class WP_Error could not be converted to int in wp-includes/post.php on line 4365
Here is the code that handles the image upload part:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file, $pid);
}
}
function insert_attachment($file_handler,$post_id,$setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
Note: This only happens when file is not uploaded. Seems like it's expecting an integer and throws an error if there is not file id or integer. How do I fix this? Thanks.
I discovered that when I went from my local server to a staging server, the error disappeared. Guessing that it might be a wp-config or htaccess file difference that caused the error, not sure, but anyway it's no longer an issue.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
hi so I used a great tutorial off of http://scribu.net/wordpress/optimal-script-loading.html and I am having trouble loading the scripts when a shortcode called lightbox is used here is my code. I am at the page when it is called wanted to check with someone to see if it is properly written.
add_shortcode('lightbox', 'lightbox_handler');
function lightbox_handler($atts) {
$base = get_stylesheet_directory_uri();
wp_enqueue_script('jquery-mousewheel', $base . '/js/jquery.mousewheel-3.0.6.pack.js', array('jquery'), null, true);
wp_enqueue_script('jquery-fancybox', $base . '/js/jquery.fancybox.pack.js', array('jquery'), '1.0', true);
wp_enqueue_script('jquery-fancybox-buttons', $base . '/js/jquery.fancybox-buttons.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-media', $base . '/js/jquery.fancybox-media.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-thumbs', $base . '/js/jquery.fancybox-thumbs.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox', $base . '/css/jquery.fancybox.css', false, '1.0', true);
wp_enqueue_style('jquery-fancybox-buttons', $base . '/css/jquery.fancybox-buttons.css', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox-thumbs', $base . '/css/jquery.fancybox-thumbs.css', array('jquery-fancybox'), '1.0', true);
}
function lightbox( $atts, $content = null ) {
$defaults = apply_filters( 'toggle_shortcode_args',
array(
'title' => '',
'id' => '',
'type' => '',
'rel' => '',
'url' => '',
)
);
extract( shortcode_atts( $defaults, $atts ) );
$html .= '<a class="'.$id.'" rel="'.$rel.'" data-fancybox-group="gallery" href="'.$url.'" title="'.$title.'"><img src="'.$url.'" alt="" />';
$html .= '</a>';
return $html;
}
First point: that tutorial is based on an old version of WordPress; don't follow that one. Now:
The handle you specify in wp_register_script() must be unique. You are reusing the same handle (fancybox) for all scripts; this won't work. Give each one a different handle, e.g. 'jquery-mousewheel', 'jquery-fancybox', 'jquery-fancybox-buttons', etc. Try to stick to established naming habits that WordPress uses, see wp_enqueue_script() default scripts for examples.
If you want your scripts to be output in the footer, just say so in wp_register_script() (which you have); there is no need to use the wp_footer action. Styles should be output in the header anyway, not the footer.
You can tell WordPress to register and enqueue scripts and stylesheets for output with one call each: wp_enqueue_script() and wp_enqueue_style(). There is no need to register, then enqueue, in your simple case.
The best action hook for registering and enqueueing scripts is 'wp_enqueue_scripts'. Try to only use that one except in special circumstances.
Bonus points: where a script name already includes the version number, you can suppress the extra '?ver=nnn' that WordPress tacks on by passing null as the version string.
And finally, get_stylesheet_directory_uri() doesn't take any arguments.
NB: this gets your script working. One thing your linked tutorial does differently is only output your scripts conditionally, but there are better tutorials on that subject linked at the bottom of the wp_enqueue_scripts() manual page. First, get your site working!
class fancy{
static function init() {
add_shortcode('lightbox', array(__CLASS__, 'handle_shortcode'));
add_action('wp_enqueue_scripts', array(__CLASS__, 'wp_enqueue_scripts'));
}
static function handle_shortcode($atts) {
self::$add_script = true;
// actual shortcode handling here
}
static function wp_enqueue_scripts() {
$base = get_stylesheet_directory_uri();
wp_enqueue_script('jquery-mousewheel', $base . '/js/jquery.mousewheel-3.0.6.pack.js', array('jquery'), null, true);
wp_enqueue_script('jquery-fancybox', $base . '/js/jquery.fancybox.pack.js', array('jquery'), '1.0', true);
wp_enqueue_script('jquery-fancybox-buttons', $base . '/js/jquery.fancybox-buttons.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-media', $base . '/js/jquery.fancybox-media.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_script('jquery-fancybox-thumbs', $base . '/js/jquery.fancybox-thumbs.js', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox', $base . '/css/jquery.fancybox.css', false, '1.0', true);
wp_enqueue_style('jquery-fancybox-buttons', $base . '/css/jquery.fancybox-buttons.css', array('jquery-fancybox'), '1.0', true);
wp_enqueue_style('jquery-fancybox-thumbs', $base . '/css/jquery.fancybox-thumbs.css', array('jquery-fancybox'), '1.0', true);
}
}
fancy::init();
PS: I suspect that jquery.fancybox.pack.js is a packed (i.e. minified) version of jquery.fancybox.js, so use that one as 'jquery-fancybox' if it is; you don't need both.
The first argument to wp_register_script needs to be unique for each script (see the codex), so you might need names like "fancybox.mousewheel", "fancybox", "fancybox.buttons" etc to differentiate. I'd check, but I suspect you only need one of fancybox.js and fancybox.pack.js - I'd think the pack file is a minified version of fancybox.js
But what version of WordPress are you using? The link at the top of the tutorial you mentioned shows a cleaner way to do it in WordPress 3.3+, removing the need for the class.