Add thumbnail description WordPress

Put below code in functions.php 

add_filter( ‘admin_post_thumbnail_html’, ‘lilahB_thumbnail_add_description’, 10, 2 );

function lilahB_thumbnail_add_description( $content, $post_id ){
$content .= “<p><b for=\”html\”>Product page (510 * 400)</b></p>”;
return $content;
return $post_id;
}

Advertisements

Add new extra fields for WooCommerce registration with role set

add_action( ‘woocommerce_register_form_start’, ‘aviweb_extra_register_fields’ );
/**
*
* Validate the extra register fields.
*
*/
function aviweb_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST[‘user_type’] ) && empty( $_POST[‘user_type’] ) ) {
$validation_errors->add( ‘user_type_error’, __( ‘<strong>Error</strong>: User type is required!.’, ‘woocommerce’ ) );
}
}
add_action( ‘woocommerce_register_post’, ‘aviweb_validate_extra_register_fields’, 10, 3 );
/**
*
* Save the extra register fields.
*
*/
function aviweb_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST[‘user_type’] ) ) {
update_user_meta( $customer_id, ‘user_type’, sanitize_text_field( $_POST[‘user_type’] ) );
$my_user = new WP_User( $customer_id );
if($_POST[‘user_type’] == “subscriber”){
$my_user->set_role( “subscriber” );
}else if($_POST[‘user_type’] == “customer”){
$my_user->set_role( “customer” );
}
}
}
add_action( ‘woocommerce_created_customer’, ‘aviweb_save_extra_register_fields’ );

Split large SQL file into small files using PHP code

Please find below code to Split large SQL file into small files

$file_handle = fopen(‘sql/file.sql’,’r’); //open big file with fopen
$f = 1; //new file number
while(!feof($file_handle))
{
$newfile = fopen(‘sql/splitfile-‘ . $f . ‘.sql’,’w’); //create new file to write to with file number
for($i = 1; $i <= 3000; $i++) //for 3000 lines
{
$import = fgets($file_handle);
fwrite($newfile,$import);
if(feof($file_handle))
{break;} //If file ends, break loop
}
fclose($newfile);
//MySQL newfile insertion stuff goes here
$f++;
}
fclose($file_handle);

MailChimp integration with PHP CURL API

$memberHash = md5($email);
$api_key = ‘XXXXXXXXXXXXXX-us19’;  //API KEY
$server = ‘us19.’;
$list_id = “XXXXXXX”; // LIST ID
$auth = base64_encode( ‘user:’.$api_key );
$data = array(
‘apikey’ => $api_key,
’email_address’ => $email,
‘status’ => ‘subscribed’,
‘merge_fields’ => array(
‘NAME’ => $name,
),
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://\’.$server.’api.mailchimp.com/3.0/lists/’.$list_id.’/members/’.$memberHash);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’,
‘Authorization: Basic ‘.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, ‘PHP-MCAPI/2.0’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “PUT”);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$result_obj = json_decode($result);
echo $result_obj->status;

WordPress custom fields for custom post type

/*Show data fields in metabox*/
add_action( ‘admin_init’, ‘my_admin_meta_data’ );
function my_admin_meta_data() {
add_meta_box( ‘custom_meta_box’,
‘Price Information’,
‘display_price_meta_box’,
‘custom-post’, ‘normal’, ‘high’
);
}

function display_price_meta_box($post_data){
$price = esc_html( get_post_meta( $post_data->ID, ‘price’, true ) );
?>

<?php
}

/*Save custom post data*/
add_action( ‘save_post’, ‘save_data_function’, 10, 2 );

function add_property_fields($post_id, $post_data){
if ( $post_data->post_type == ‘custom-post’ ) {
if ( isset( $_POST[‘price’] ) && $_POST[‘price’] != ” ) {
update_post_meta( $post_id, ‘price’, $_POST[‘price’] );
}
}
}
?>