Skip to content

Instantly share code, notes, and snippets.

View amrt0510's full-sized avatar

Amritansh Trivedi amrt0510

View GitHub Profile

yy

@amrt0510
amrt0510 / add_script_in_admin
Created September 26, 2017 05:47
adding custom js to specific post type add or edit
// hooking script for custom post type 'homework'
add_action( 'admin_print_scripts-post-new.php', 'homework_admin_script', 11 );
add_action( 'admin_print_scripts-post.php', 'homework_admin_script', 11 );
function homework_admin_script() {
global $post_type;
if( 'homework' == $post_type )
wp_enqueue_script( 'homework-admin-script', get_bloginfo('template_directory') . '/js/admin_homework.js' );
}
@amrt0510
amrt0510 / add_product_to_cart.php
Created September 26, 2017 05:45
This function add a product into the woocommerce cart
add_product_to_cart($product_id, $quantity, $variation_id, $variation, $cart_item_data);
function add_product_to_cart($product_id, $default_product_quantity, $variation_id, $variation = array(), $cart_item_data = array()) {
//check if product already in cart
if (sizeof(WC()->cart->get_cart()) > 0) {
WC()->session->set("cart_strorage", WC()->cart->get_cart());
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if ($_product->id == $product_id) {
$found = true;
@amrt0510
amrt0510 / attach_the_terms_to_a_post.php
Created September 26, 2017 05:45
This function attach terms to a post
attach_the_terms_to_a_post($post_id, $taxanomy_name, $combine_term_ids);
function attach_the_terms_to_a_post($post_id, $taxanomy_name, $combine_term_ids) {
$combine_term_ids = array_map('intval', $combine_term_ids);
$result = wp_set_object_terms($post_id, $combine_term_ids, $taxanomy_name);
return $result;
}
@amrt0510
amrt0510 / check_if_a_post_is_inserted.php
Created September 26, 2017 05:45
This function checks if a post is inserted in the wp database based on a metakey and a metavalue, and return its postid if it is present
/*
* This function checks if a post is inserted in the wp database based on a metakey and a metavalue, and return its postid if it is present
* $is_meta_value_unique contains true or false. if true is passed then one postid is returned else array of postids is returned
*/
check_if_a_post_is_inserted($meta_key_name, $meta_key_value, $posttype, $is_meta_value_unique);
function check_if_a_post_is_inserted($meta_key_name, $meta_key_value, $posttype, $is_meta_value_unique) {
if ($is_meta_value_unique) {
$posts_per_page = 1;
@amrt0510
amrt0510 / convert_metato_term.php
Created September 26, 2017 05:44
This function converts meta of a post into the term
/*
* This function converts meta of a post into the term
*/
convert_metato_term($posttype, $taxanomy_name, $meta_key_name);
function convert_metato_term($posttype, $taxanomy_name, $meta_key_name) {
global $post;
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
@amrt0510
amrt0510 / custom_breadcrumbs.php
Created September 26, 2017 05:44
This function adds breadcrumbs
/*
* This function adds breadcrumbs
*/
custom_breadcrumbs($separator, $breadcrums_id, $breadcrums_class, $home_title, $custom_taxonomy, $search);
function custom_breadcrumbs($separator, $breadcrums_id, $breadcrums_class, $home_title, $custom_taxonomy, $search = NULL) {
// Settings
// $separator = '>';
// $breadcrums_id = 'breadcrumbs';
@amrt0510
amrt0510 / delete_all_posts_of_a_postype.php
Created September 26, 2017 05:44
Deletes all the posts of a posttype
/*
* Deletes all the posts of a posttype
*/
delete_all_posts_of_a_postype($posttype, $posts_per_page);
function delete_all_events() {
global $post;
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
@amrt0510
amrt0510 / delete_metakey.php
Created September 26, 2017 05:44
Delete a metakey and its value from wordpress db based on comparison operator
/*
* Delete a metakey and its value from wordpress db based on comparison operator
* $comparison_operator can contain 'Like' or '='
*/
delete_metakey($metakey_to_delete, $comparison_operator);
function delete_metakey($metakey_to_delete, $comparison_operator) {
global $wpdb;
if ($comparison_operator == 'LIKE') {
$mysql_query = "DELETE FROM $wpdb->postmeta WHERE meta_key " . $comparison_operator . " '%" . $metakey_to_delete . "%'";
@amrt0510
amrt0510 / getPostViews.php
Created September 26, 2017 05:44
This function returns the post count views of a posttype
/*
* This function returns the post count views of a posttype
*/
getPostViews($postID);
function getPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
delete_post_meta($postID, $count_key);