so i'm trying about 2 3 with this and i can't managed to make it work.
I have this theme : Aruna and he has front end post and when i'm posting a image
i need to write title and upload a image from my computer ,
ok after i done this , he put the image that i upload into one custom field by theme:
The trick is when i want to upload a image from front end i wanna put it automatically on Featured image.
If i use User submited post plugin and i post from him it works .. but i wanna modifiy on my own front end.
It's important for me to make this work because i share a lot on facebook and other social media and feature image do the thing :)
I don't know what code to put here but i try to show you :
Functions.php
case "window-image":
$format = 'image';
$title = esc_html($_POST['title_image']);
$image = esc_url($_POST['postimage']);
$tags = $_POST['tags_image'];
$comments = isset($_POST['comments_image']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_image']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if($image != '') {
$pattern = '~' . get_bloginfo('url') . '~';
if(preg_match($pattern, $image) ) {
add_post_meta($post_id, '_standard_image', $image);
}
else {
//we have an external image :) we save it with the cURL library
$filedetails = pathinfo($image);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($image);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);
}
}
}
else {
if($_FILES['image_imagepost']['error'] == 0) {
$image = $tmp->saveUpload( 'image_imagepost' );
add_post_meta($post_id, '_standard_image', wp_get_attachment_url($image['attachment_id']) );
}
}
HTML code on front end
<textarea name="title_image" class="form-control" rows="1" placeholder="<?php _e('Post title', 'Aruna');?>"></textarea>
<input type="file" name="image_imagepost" />
<input type="file" name="image_imagepost" /> this do the trick for upload on custom field ...
I can't understand , when i'm posting videos(yt) he take the thumbnail automatically and put it on featured image, I try to take a look at video and image and unsuccessful:
Here is the video code :
<?php
case "window-video":
$format = 'video';
$title = esc_html($_POST['title_video']);
$tags = $_POST['tags_video'];
if($title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$comments = isset($_POST['comments_video']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_video']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if(isset($_FILES['video_file']) && $_FILES['video_file']['error'] == 0) {
$video_file = $tmp->saveUpload( 'video_file' );
add_post_meta($post_id, '_video_localvideo', wp_get_attachment_url($video_file['attachment_id']) );
}
else {
$video = esc_url($_POST['video']);
if (strpos($video,'youtube') !== false) {
//it's youtube, we need toget the thumbnail! :)
//we get the id of the video first
$videotemp = explode('/watch?v=', $video);
if(isset($videotemp[1]) && $videotemp[1] != '') {
$idvideo = $videotemp[1];
//we have the id, now we put the thumbnail
$url_video = 'http://i1.ytimg.com/vi/' . $idvideo . '/hqdefault.jpg';
}
}
elseif (strpos($video,'vimeo') !== false) {
//it's youtube, we need toget the thumbnail! :)
//we get the id of the video first
$videotemp = explode('vimeo.com/', $video);
if(isset($videotemp[1]) && $videotemp[1] != '') {
$idvideo = $videotemp[1];
//we have the id, now we put the thumbnail
$json_vimeo = 'http://vimeo.com/api/v2/video/' . $idvideo . '.json';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $json_vimeo);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$json_vimeo = curl_exec($ch);
curl_close($ch);
$jsontemp = json_decode($json_vimeo, true);
if(isset($jsontemp[0]) && isset($jsontemp[0]['thumbnail_large']) && $jsontemp[0]['thumbnail_large'] != '') {
$url_video = $jsontemp[0]['thumbnail_large'];
}
}
}
if(isset($url_video) && $url_video != '') {
//we have an external youtube image :) we save it with the cURL library
$filedetails = pathinfo($url_video);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($url_video);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($post_id, '_thumbnail_id', $attach_id);
}
}
add_post_meta($post_id, '_video_externalvideo', $video);
}
$nsfw = isset($_POST['nsfw_video']) ? 1 : 2;
add_post_meta($post_id, '_video_nsfw', $nsfw);
add_post_meta($post_id, '_anonymous', $anonymous);
?>
and here is the image code :
case "window-image":
$format = 'image';
$title = esc_html($_POST['title_image']);
$image = esc_url($_POST['postimage']);
$tags = $_POST['tags_image'];
$comments = isset($_POST['comments_image']) ? 1 : 0;
$anonymous = isset($_POST['anonymous_image']) ? 1 : 0;
$comment_status = $comments == 1 ? 'open' : 'closed';
$user_id = $id;
if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
wp_redirect(home_url('/') . '?posterror=1');
exit;
}
$post = array(
'comment_status' => $comment_status,
'ping_status' => 'open',
'post_author' => $user_id,
'post_content' => '',
'post_status' => $status,
'post_title' => $title,
'post_type' => 'post',
);
$post_id = wp_insert_post($post);
if($image != '') {
$pattern = '~' . get_bloginfo('url') . '~';
if(preg_match($pattern, $image) ) {
add_post_meta($post_id, '_standard_image', $image);
}
else {
//we have an external image :) we save it with the cURL library
$filedetails = pathinfo($image);
$mime = '';
if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
$mime = 'image/jpeg';
}
elseif($filedetails['extension'] == 'png') {
$mime = 'image/png';
}
elseif($filedetails['extension'] == 'gif') {
$mime = 'image/gif';
}
if($mime != '') {
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$ch = curl_init($image);
$uploaddir = wp_upload_dir();
$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension'];
//we add some random digits to make sure it's unique
$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$guid = trailingslashit($uploaddir['url']) . $filename;
$attachment = array(
'post_mime_type' => $mime,
'guid' => $guid,
'post_title' => '[External image] : ' . $filedetails['filename'] ,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'inherit',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' )
);
// Add the file to the media library and generate thumbnail.
$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);
}
}
}
else {
if($_FILES['image_imagepost']['error'] == 0) {
$image = $tmp->saveUpload( 'image_imagepost' );
add_post_meta($post_id, '_standard_image', wp_get_attachment_url($image['attachment_id']) );
}
}
$nsfw = isset($_POST['nsfw_image']) ? 1 : 2;
add_post_meta($post_id, '_standard_nsfw', $nsfw);
add_post_meta($post_id, '_anonymous', $anonymous);
break;
Related
woocommerce order page in, I want to filter orders by vendor. I am using the wcfm marketplace plugin.
Vendor name appears here. but when I filter, the orders are not listed.
please help :(woocommerce order page
function wcmp_admin_filter_by_vendor() {
global $typenow;
if ($typenow == 'shop_order') {
$admin_dd_html = '<select name="admin_order_vendor" id="dropdown_admin_order_vendor"><option value="">'.__("Show All Vendors", "dc-woocommerce-multi-vendor").'</option>';
$vendors = get_wcmp_vendors();
if($vendors) :
foreach ($vendors as $vendor) {
$admin_dd_html .= '<option value="'.$vendor->term_id.'">'.$vendor->page_title.'</option>';
}
endif;
$admin_dd_html .= '</select>';
echo $admin_dd_html;
}
}
add_action( 'restrict_manage_posts', 'wcmp_admin_filter_by_vendor');
function get_vendor_parent_order($id) {
$vendor_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_vendor_id',
'meta_value' => $id,
'post_type' => 'shop_order',
'post_status' => 'any',
) );
foreach( $vendor_orders as $vendor_order ) {
$parent_order = wp_get_post_parent_id( $vendor_order->ID );
$parent_orders[] = $parent_order;
}
return $parent_orders;
}
function filter_orders_by_vendor_in_admin_dashboard( $query ) {
if (current_user_can('administrator') && !empty($_REQUEST['admin_order_vendor'])) {
$vendor_term_id = isset($_GET['admin_order_vendor'])?$_GET['admin_order_vendor']:'';
$vendor = get_wcmp_vendor_by_term($vendor_term_id);
$parent_orders = get_vendor_parent_order($vendor->id);
$query['post__in'] = $parent_orders;
return $query;
}
return $query;
}
add_filter( 'wcmp_shop_order_query_request', 'filter_orders_by_vendor_in_admin_dashboard');
function remove_wcmp_order_hook() {
global $WCMp;
remove_action( 'manage_shop_order_posts_custom_column', array($WCMp->order, 'wcmp_show_shop_order_columns'), 99, 2 );
}
add_action('init', 'remove_wcmp_order_hook');
function wcmp_show_shop_order_columns($column, $post_id) {
global $WCMp;
switch ($column) {
case 'wcmp_suborder' :
$wcmp_suborders = $WCMp->order->get_suborders($post_id);
if ($wcmp_suborders) {
echo '<ul class="wcmp-order-vendor" style="margin:0px;">';
foreach ($wcmp_suborders as $suborder) {
$vendor = get_wcmp_vendor(get_post_field('post_author', $suborder->get_id()));
$vendor->ID = get_post_field('post_author', $suborder->get_id());
$vendor_term_id = isset($_GET['admin_order_vendor'])?$_GET['admin_order_vendor']:'';
$filter_vendor = get_wcmp_vendor_by_term($vendor_term_id);
$filter_vendor_id = isset($filter_vendor->id)?$filter_vendor->id:'';
if( $vendor->ID == $filter_vendor_id || $filter_vendor_id == '' ) {
$order_uri = apply_filters('wcmp_admin_vendor_shop_order_edit_url', esc_url('post.php?post=' . $suborder->get_id() . '&action=edit'), $suborder->get_id());
printf('<li><mark class="%s tips" data-tip="%s">%s</mark> <strong>#%s</strong> – <small class="wcmp-order-for-vendor">%s %s</small></li>', sanitize_title($suborder->get_status()), $suborder->get_status(), $suborder->get_status(), $order_uri, $suborder->get_order_number(), _x('for', 'Order table details', 'dc-woocommerce-multi-vendor'), $vendor->page_title
);
}
do_action('wcmp_after_suborder_details', $suborder);
}
echo '<ul>';
} else {
echo '<span class="na">–</span>';
}
break;
}
}
add_action('manage_shop_order_posts_custom_column', 'wcmp_show_shop_order_columns', 99, 2);
I am trying to update my repeater field from front end-with files. In case with one field file - it works. But when I'm trying to fill repeater with files it doesn't work.
// JS. Ajaxly sending files
var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php';
$('.js-new-msg-send').click(function (e) {
e.preventDefault();
var form = document.forms.namedItem("new_theme");
var formData = new FormData(form);
formData.append('user_id', '<?php echo $userID; ?>');
formData.append('action', 'msg_to_acf');
var xhr = new XMLHttpRequest();
xhr.open('POST', ajaxurl, true);
xhr.send(formData);
})
I found function to handle files uploading to acf field and modified it to handle multiple files from $_FILES variable:
// FOR MULTIPLE FILES
if ( !empty($_FILES[$f]['name']) && is_array($_FILES[$f]['name']) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$file_list = array();
foreach ($_FILES[$f]['name'] as $key => $value) {
$temp_file = array(
'name' => $_FILES[$f]['name'][$key],
'type' => $_FILES[$f]['type'][$key],
'tmp_name' => $_FILES[$f]['tmp_name'][$key],
'error' => $_FILES[$f]['error'][$key],
'size' => $_FILES[$f]['size'][$key]
);
wp_update_attachment_metadata( $pid, $temp_file );
$file = wp_handle_upload( $temp_file , array('test_form' => FALSE, 'action' => 'editpost') );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($temp_file['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $file['filename'];
$type = $file['type'];
$title = $t ? $t : $name;
$content = $c;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $pid,
'post_mime_type' => $type,
'guid' => $file['url'],
);
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
if ( !is_wp_error( $id )) {
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
$final_temp_arr = array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
array_push($file_list, $final_temp_arr);
}
}
return $file_list;
}
In final, I using this function to get an array of information with attach-id variable that I inserting to repeater.
$att = my_update_attachment( 'msg-file-list' , $post_id );
$files_final_list = array();
foreach ($att as $key => $val) {
array_push($files_final_list, $val['attach_id']);
}
$value_arr = array(
array(
"msg-author" => $user_name,
"msg-date" => $date,
"msg-read-check" => true,
"msg-cont" => $msg_cont,
'msg-file-list' => $files_final_list
)
);
update_field( 'test_file_list' , $files_final_list , $post_id );
I guess that mistake is in last part , where I trying to upload files to repeater. Can someone show me what I'am doing wrong? Thanks for helping.
I'm trying to update remote woocommerce product but i'm stucked. How can we complete this code:
class myDemo {
public function __construct() {
$this->url = 'http://localhost/alfa/wp-json/wc/v2/products/2213';
$this->meta_key = 'whatever';
$this->consumer_key = 'ck_5978846499ea9bb1554b420e59072e46b8dedb1d';
$this->consumer_secret = 'cs_1009590c5443306f2082b2e508098be37fba2ee5';
$this->access_token = '';
$this->access_token_secret = '';
$this->method = 'PUT';
$this->set_signature_key();
$this->set_nonce();
$this->set_headers();
$this->set_base_string();
$this->set_signature();
$this->set_headers();
$this->set_header_string();
}
function get_response() {
$url = $this->url;
if( ! empty( $this->meta_key ) ) {
$url = add_query_arg( array( 'meta_key' => $this->meta_key ), $url );
}
$args = array(
'method' => $this->method,
'headers' => array(
'Authorization' => 'OAuth ' . $this->header_string,
),
);
$out = wp_remote_request( $url, $args );
return $out;
}
function set_signature_key() {
$this->signature_key = urlencode( $this->consumer_secret ) . '&' . urlencode( $this->access_token_secret );
}
function set_nonce() {
$this->nonce = wp_create_nonce( rand() . $this->url . $this->method );
}
function set_headers() {
if( ! isset( $this->headers ) ) {
$this->headers = array(
'oauth_consumer_key' => $this->consumer_key,
'oauth_nonce' => $this->nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $this->access_token,
'oauth_version' => '1.0',
);
} elseif( isset( $this->signature ) ) {
$this->headers['oauth_signature'] = $this->signature;
}
}
function set_base_string() {
$headers = $this->headers;
$url_params = array(
'meta_key' => $this->meta_key
);
$headers_and_params = array_merge( $headers, $url_params );
ksort( $headers_and_params );
$headers_and_params_string = '';
foreach( $headers_and_params as $key => $value ) {
$headers_and_params_string .= "$key=$value&";
}
$headers_and_params_string = rtrim( $headers_and_params_string, '&' );
$out = $this->method . '&' . rawurlencode( $this->url ) . '&' . rawurlencode( $headers_and_params_string );
$this->base_string = $out;
}
function set_signature() {
$out = hash_hmac( 'sha1', $this->base_string, $this->signature_key, TRUE );
$out = base64_encode( $out );
$this->signature = $out;
}
function set_header_string() {
// Will hold the combined string of headers.
$out = '';
foreach( $this->headers as $key => $value ) {
$value = rawurlencode( $value );
$out .= $key . '=' . '"' . $value . '"' . ', ';
}
$out = rtrim( $out, ', ' );
$this->header_string = $out;
}
}
$demo = new myDemo();
die(json_encode($demo->get_response()));
I'm interesting with that:
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product
I saw this code part:
$data = [
'regular_price' => '24.54'
];
I must send this data array to remote woocommerce. But i don't know how.
Thanks for everything.
In Wordpress, I'm looking to get the depth a category's subcategories.
Suppose I have a category 'grandparent' which has a subcategory 'parent' which in turn has a subcategory 'child'. How do I get to integer 2?
I suppose I could check if 'grandparent' has subcategories and if it does, check if they have subcategories, etc, until I hit 0. But that seems like a lot of unnecessary processing.
Isn't there a more elegant way?
There is a nice blog post here that shows you how to build a function that will get you the depth of a category but not only... check the page the function has more options.
EDIT: code here:
function get_depth($id = '', $depth = '', $i = 0) {
global $wpdb;
if($depth == '') {
if(is_page()) {
if($id == '') {
global $post;
$id = $post->ID;
}
$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'");
return get_depth($id, $depth, $i);
}
elseif(is_category()) {
if($id == '') {
global $cat;
$id = $cat;
}
$depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'");
return get_depth($id, $depth, $i);
}
elseif(is_single()) {
if($id == '') {
$category = get_the_category();
$id = $category[0]->cat_ID;
}
$depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'");
return get_depth($id, $depth, $i);
}
}
elseif($depth == '0') {
return $i;
}
elseif(is_single() || is_category()) {
$depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$depth."'");
$i++;
return get_depth($id, $depth, $i);
}
elseif(is_page()) {
$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'");
$i++;
return get_depth($id, $depth, $i);
}
}
Assuming $cat to be the category ID of 'grandparent':
$number_of_subcategories = count(get_categories("echo=0&cat=" . $cat));
May help?
http://codex.wordpress.org/Function_Reference/get_categories
Try this one :
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 0,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
if($cat->parent == 0) {
$parent_cat = null;
$head = $cat->name;
$head_id = $cat->term_id;
}
echo "<ul><a class='parent-category' href=''>" . $head . "</a>";
wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$head_id}&show_option_none=");
echo "</ul>";
}
Thanks.
i created a code which publish product from front-end issue is when i upload gallery images from frontend only last image upload.
here is code
<form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple>
here is function multiupload
agp_process_image('agp_image_file', $post_id, $result['caption'], $result['content'], $result['author']);
if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("agp_gallery" => $file);
foreach ($_FILES as $file => $array) {
agp_process_wooimage($file, $post_id, $result['caption']);
}
}
}
}
here is main function
function agp_process_wooimage($file, $post_id, $caption){
if ($_FILES[$file]['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');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, '_product_image_gallery', $attachment_id);
$attachment_data = array(
'ID' => $attachment_id,
'post_excerpt' => $caption
);
wp_update_post($attachment_data);
return $attachment_id;
}
here is a final code :-
// Get the upload attachment files
if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("agp_gallery" => $file);
foreach ($_FILES as $file => $array)
{
$newupload = agp_process_wooimage($file,$post_id);
}
}
}
}
function agp_process_wooimage($file, $post_id){
if ($_FILES[$file]['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');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $attachment_id));
return $attachment_id;
}