<?php // add mnue by user role
$args = array(
'role' => 'content_writer'
);
$users = get_users($args);
function report_register_my_custom_menu_page()
{
add_menu_page(
__('Task Reporting', 'textdomain'),
'Task Reporting',
'edit_others_posts',
'report',
'report_function',
'dashicons-admin-page',
6
);
$user = wp_get_current_user();
if ($user->roles[0] == 'administrator') {
add_menu_page(
__('Content Writer Report', 'textdomain'),
'Content Writer Report',
'manage_options',
'content_report',
'content_report_function',
'dashicons-admin-page',
6
);
}
if ($user->roles[0] == 'project_manager') {
add_menu_page(
__('Content Writer Report', 'textdomain'),
'Content Writer Report',
'edit_others_posts',
'content_report',
'content_report_function',
'dashicons-admin-page',
6
);
}
if ($user->roles[0] == 'content_writer') {
add_menu_page(
__('Content Writer Report', 'textdomain'),
'Content Writer Report',
'edit_others_posts',
'content_report',
'content_report_function_user',
'dashicons-admin-page',
6
);
}
}
add_action('admin_menu', 'report_register_my_custom_menu_page');
$user = wp_get_current_user();
if ($user->roles[0] == 'administrator' || $user->roles[0] == 'project_manager' || $user->roles[0] == 'content_writer') {
$args = array(
'post_type' => 'order_history',
'posts_per_page' => '-1',
);
}
?>
Blog Feed
filter by category code Ajex
<select id="category_select">
<option value="">Select Category</option>
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<option value="' . $category->cat_ID . '">' . $category->name . '</option>';
}
?>
</select>
<?php
// Add AJAX action for authenticated users
add_action('wp_ajax_filter_posts_by_category', 'filter_posts_by_category_callback');
// Add AJAX action for non-authenticated users
add_action('wp_ajax_nopriv_filter_posts_by_category', 'filter_posts_by_category_callback');
function filter_posts_by_category_callback() {
$category_id = intval($_POST['category_id']);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'cat' => $category_id // Filter posts by category ID
);
$posts = get_posts($args);
$response = array();
foreach ($posts as $post) {
$response[] = array(
'title' => $post->post_title,
'content' => $post->post_content
);
}
wp_send_json($response);
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#category_select').change(function() {
var category_id = $(this).val();
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'filter_posts_by_category',
category_id: category_id
},
success: function(response) {
$('#posts_container').empty(); // Clear previous posts
if (response) {
response.forEach(function(post) {
$('#posts_container').append('<div class="post"><h2>' + post.title + '</h2><p>' + post.content + '</p></div>');
console.log(post);
});
} else {
$('#posts_container').html('<p>No posts found</p>');
}
},
error: function(xhr, status, error) {
console.error('AJAX Error:', error);
}
});
});
});
</script>
Get Post data by api
<?php
$url = 'http://localhost/demoproject/api/get_posts/';
$json = wp_remote_get($url);
$json_body = wp_remote_retrieve_body($json);
$data = json_decode($json_body, true);
/*echo "<pre/>";
print_r($data);*/
//$parsed['posts']['id'];
// Check if posts array exists and it is not empty
if (isset($data['posts']) && !empty($data['posts'])) {
foreach ($data['posts'] as $post) {
echo '<h2>' . $post['title'] . '</h2> <br/>';
echo '<p>' . $post['content'] . '</p> <br/>';
echo '<p>Date: ' . $post['date'] . '</p><br/>';
echo '<p>Author: ' . $post['author']['name'] . '</p>';
echo '<p>Author: ' . $post['author']['first_name'] . '</p>';
echo '<p>Author: ' . $post['author']['last_name'] . '</p>';
echo '<hr>';
}
} else {
echo 'No posts found.';
}
//foreach ($listings as $listing){ //insert data into custom fields}
?>
Add custom woocommerce checkout fields
<?php
// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['shipping_phone'] = array(
'label' => __( 'Phone1', 'woocommerce' ),
'placeholder' => _x( 'Phone1', 'placeholder', 'woocommerce' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'. esc_html__( 'Phone From Checkout Form' ) . ':</strong> ' . esc_html( $order->get_meta( '_shipping_phone', true ) ) . '</p>';
}
?>
Display discount on shop page woocommerc
// Display discount on shop page
add_action( 'woocommerce_after_shop_loop_item_title', 'display_discount_on_shop_page', 15 );
function display_discount_on_shop_page() {
global $product;
// Check if the product is on sale
if ( $product->is_on_sale() && ($product->is_type('simple')) ) {
// Get regular price and sale price
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
// Calculate discount percentage
$discount_percentage = round( ( ($regular_price - $sale_price) / $regular_price ) * 100 );
// Display discount percentage
echo '<span class="discount-badge">' . $discount_percentage . '% Off</span>';
}
}
Display woocommerc shop page Gallery item
<style>
flex-control-thumbs {
width: 90%;
position: absolute;
bottom: 0px;
text-align: center;
display:flex;
flex-wrap:nowrap;
border: 1px solid red;
overflow-x:auto;
padding-bottom:10px;
}
.flex-control-thumbs li {
margin: 0 6px;
display: inline-block;
zoom: 1;
}
.flex-control-thumbs li,
.flex-control-thumbs li:first-child {
width: 10%;
vertical-align: top;
margin: 5px 1% 0 0;
min-width: 60px;
}
.flex-control-thumbs img {
width: 100%;
display: block;
opacity: 0.8;
cursor: pointer;
}
.flex-control-thumbs img:hover {
opacity: 0.5;
}
.flex-control-thumbs .flex-active {
opacity: 1;
cursor: default;
}
.product_slider .flex-active-slide a:hover {
cursor: -webkit-zoom-in;
cursor: -moz-zoom-in;
}
ul.flex-direction-nav {
position: absolute;
top: 30%;
z-index: 99999;
width: 100%;
left: 0;
margin: 0;
padding: 0px;
list-style: none;}
li.flex-nav-prev {float: left;}
li.flex-nav-next {float: right;}
a.flex-next {visibility:hidden;}
a.flex-prev {visibility:hidden;}
a.flex-next::after {visibility:visible;content: '\f105';
font-family: FontAwesome;margin-right: 10px;font-size: 70px; }
a.flex-prev::before {
visibility:visible;
content: '\f104';
font-family: FontAwesome; margin-left: 10px;font-size: 70px;
</style>
add_action('woocommerce_shop_loop_item_title','wps_add_extra_product_thumbs', 15);
function wps_add_extra_product_thumbs() {
if ( is_shop() ) {
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
wp_enqueue_script('flexslider');
echo '<div class="flexslider"><ul class="flex-control-nav flex-control-thumbs product-thumbs slides">';
foreach( array_slice( $attachment_ids, 0,3 ) as $attachment_id ) {
$thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' )[0];
echo '<li data-thumb="' . $thumbnail_url . '">';
echo '<img src="' . $thumbnail_url . '">';
echo '</li>';
}
echo '</ul></div>';
}
}
Quantity inputs for woocommerce on shop page
<?php
//Quantity inputs for woocommerce on shop page
// Ensure WooCommerce is active
if ( class_exists( 'WooCommerce' ) ) {
// Ensure Ajax add to cart is enabled
function custom_ajax_add_to_cart() {
if ( is_shop() || is_product_category() || is_product_tag() ) {
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart', 10, 2 );
}
}
add_action( 'woocommerce_before_shop_loop', 'custom_ajax_add_to_cart' );
function quantity_inputs_for_woocommerce_loop_add_to_cart( $html, $product ) {
if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
}
?>
Get Purchase order count last 24 hours In woocommerce
<?php // Get Purchase order count last 24 hours In woocommerce
function get_purchase_order_count_last_24_hours() {
// Set up arguments for order query
$args = array(
'date_query' => array(
array(
'after' => date( 'Y-m-d', strtotime( '-1 day' ) ) // 24 hours ago
)
),
'status' => array( 'completed', 'processing' ),
'limit' => -1, // Get all orders
);
// Get orders with specified criteria
$orders = wc_get_orders( $args );
// Count the number of orders
$order_count = count( $orders );
return $order_count;
}
$purchase_order_count_last_24_hours = get_purchase_order_count_last_24_hours();
echo "There were $purchase_order_count_last_24_hours orders in the last 24 hours.";
?>
Product Panel Limit & Columns
<div id="products-new-arrivals">
<h3>New Arrivals</h3>
<?php
$new_arrival_limit = get_theme_mod("set_new_arrival_limit");
$new_arrival_columns = get_theme_mod("set_new_arrival_column");
?>
<?php// echo do_shortcode('[products limit="'.$new_arrival_limit.'" columns="'.$new_arrival_columns.'" orderby="date" class="new-arrival-custom-class"]'); ?>
</div>
<div id="products-popularity">
<h3>Popularity</h3>
<?php
$popularity_limit = get_theme_mod("set_popular_limit");
$popularity_columns = get_theme_mod("set_popular_columns");
?>
<?php echo do_shortcode('[products limit="'.$popularity_limit.'" columns="'.$popularity_columns.'" orderby="popularity"]'); ?>
</div>
function product_bootstrap_theme_load_wp_customizer($wp_customize)
{
/// customizer code
// adding section
$wp_customize->add_section("sec_copyright", array(
"title" => "Copyright Section",
"description" => "This is a copyright section",
));
// adding settings/field
$wp_customize->add_setting("set_copyright", array(
"type" => "theme_mod",
"default" => "",
"sanitize_callback" => "sanitize_text_field",
));
// add control
$wp_customize->add_control("set_copyright", array(
"label" => "Copyright",
"description" => "Please fill the copyright text",
"section" => "sec_copyright",
"type" => "text",
));
/* section of new arrival / popularity control limit and columns */
// adding section
$wp_customize->add_section("sec_product_panel", array(
"title" => "Product Panel Limit & Columns",
"description" => "This is a section which is going to provide the controls for home page product panels",
));
// adding settings/field
$wp_customize->add_setting("set_new_arrival_limit", array(
"type" => "theme_mod",
"default" => "",
"sanitize_callback" => "absint",
));
// add control
$wp_customize->add_control("set_new_arrival_limit", array(
"label" => "New Arrival - Product Limit",
"description" => "Please fill provide the limit of new arrival",
"section" => "sec_product_panel",
"type" => "number",
));
// adding settings/field
$wp_customize->add_setting("set_new_arrival_column", array(
"type" => "theme_mod",
"default" => "",
"sanitize_callback" => "absint",
));
// add control
$wp_customize->add_control("set_new_arrival_column", array(
"label" => "New Arrival - Product Columns",
"description" => "Please fill provide the columns of new arrival",
"section" => "sec_product_panel",
"type" => "number",
));
// adding settings/field
$wp_customize->add_setting("set_popular_limit", array(
"type" => "theme_mod",
"default" => "",
"sanitize_callback" => "absint",
));
// add control
$wp_customize->add_control("set_popular_limit", array(
"label" => "Popularity - Product Limit",
"description" => "Please fill provide the limit of popularity",
"section" => "sec_product_panel",
"type" => "number",
));
// adding settings/field
$wp_customize->add_setting("set_popular_columns", array(
"type" => "theme_mod",
"default" => "",
"sanitize_callback" => "absint",
));
// add control
$wp_customize->add_control("set_popular_columns", array(
"label" => "Popularity - Product Columns",
"description" => "Please fill provide the columns of popularity",
"section" => "sec_product_panel",
"type" => "number",
));
}
add_action("customize_register", "product_bootstrap_theme_load_wp_customizer");
Nav Menu Class
<?php
function special_nav_class($classes, $item){
if( in_array('current-menu-item', $classes) ){
$classes[] = 'active';
}
return $classes;
}
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function add_additional_class_on_li($classes, $item, $args) {
if(isset($args->add_li_class)) {
$classes[] = $args->add_li_class;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_additional_class_on_li', 1, 3);
function add_link_atts($atts) {
$atts['class'] = "nav-link";
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_link_atts');
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
}
}
add_filter( 'nav_menu_link_attributes', 'nav_menu_link_class', 10, 3 );
function nav_menu_link_class( $atts, $item, $args ) {
$class = 'dropdown-item';
$atts['data-bs-toggle'] = 'dropdown';
$atts['class'] = $class;
return $atts;
}
?>