Skip to content

Instantly share code, notes, and snippets.

View mdjwel's full-sized avatar
🎯
Focusing

Eh Jewel mdjwel

🎯
Focusing
View GitHub Profile
@mdjwel
mdjwel / the_first_category_name.php
Last active January 27, 2019 19:47
Get the first category name and link in WordPress post's while loop.
<?php
// Get the 1ast category link
function Chaoz_first_category_link() {
$cats = get_the_terms(get_the_ID(), 'category');
$cat = is_array($cats) ? get_category_link($cats[0]->term_id) : '';
echo esc_url($cat);
}
// Get the first category link
function Chaoz_first_category_link() {
@mdjwel
mdjwel / get_post_views.php
Created May 28, 2018 19:49
Get post views count of WordPress single post (without plugin)
<?php
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return ($count/2).' Views';
@mdjwel
mdjwel / related_posts.php
Last active December 24, 2018 10:37
Get related posts in single post page in WordPress.
@mdjwel
mdjwel / social_networks_share_urls.php
Last active July 25, 2018 18:01
A collection of social site's share URL
<?php
function chaoz_social_share() { ?>
<a href="https://facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>"><i class="social_facebook"></i></a>
<a href="https://twitter.com/intent/tweet?text=<?php the_permalink(); ?>"><i class="social_twitter"></i></a>
<a href="https://www.pinterest.com/pin/create/button/?url=<?php the_permalink() ?>"><i class="social_pinterest"></i></a>
<a href="https://plus.google.com/share?url=<?php the_permalink() ?>"><i class="social_google_plus"></i></a>
<a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php the_permalink() ?>"><i class="social_linkedin"></i></a>
<?php
}
@mdjwel
mdjwel / custom_post.php
Last active June 28, 2018 05:15
WordPress custom post register class
<?php
// event.pt.php
/**
* Use namespace to avoid conflict
*/
namespace PostType;
/**
* Class Portfolio
@mdjwel
mdjwel / cf7_remove_autop.php
Created July 4, 2018 06:55
Remove auto p from Contact Form 7 shortcode output
<?php
// Remove auto p from Contact Form 7 shortcode output
add_filter('wpcf7_autop_or_not', 'wpcf7_autop_return_false');
function wpcf7_autop_return_false() {
return false;
}
@mdjwel
mdjwel / wc_featured_prodcuts_query.php
Created July 22, 2018 09:52
Get WooCommerce featured products Query
@mdjwel
mdjwel / icon-class-to-icon-name-array.php
Created July 24, 2018 21:08
Convert icon classes to icon names.
<?php
function chaoz_icon_array($k) {
$v = array();
foreach ($k as $kv) {
$kv = str_replace('-', ' ', $kv);
$kv = str_replace('icon', '', $kv);
$v[] = array_push($v, ucwords($kv));
}
foreach($v as $key => $value) if($key&1) unset($v[$key]);
return array_combine($k, $v);
@mdjwel
mdjwel / post_titles_array.php
Created August 4, 2018 06:26
Get post all post titles of a post type in a associative array of post_title => ID pair in WordPress
<?php
// query for your post type
$products = new WP_Query(
array (
'post_type' => 'product',
'posts_per_page' => -1
)
);
// we need the array of posts
$posts_array = $products->posts;
@mdjwel
mdjwel / get_user_role.php
Last active September 18, 2018 05:29
Get the user role in WordPress
<?php
function prefix_user_role() {
$user_meta = get_userdata(get_the_author_meta( 'ID' ));
$user_roles = $user_meta->roles; //array of roles the user is part of.
echo !empty($user_roles[0]) ? $user_roles[0] : '';
}