Monday 28 October 2024

Woocommerce product review tab rename on single product page

 add_filter('woocommerce_product_tabs', 'change_additional_information_tab_title', 98);

function change_additional_information_tab_title($tabs) {

    if ( isset($tabs['additional_information']) ) {

        $tabs['additional_information']['title'] = __('Ytterligere informasjon', 'tab-additional_information');

    }

if ( isset($tabs['reviews']) ) {

        $tabs['reviews']['title'] = __('Anmeldelser', 'reviews'); // Change 'Your New Review Title' to your desired title

    }

    return $tabs;

}

Checkbox in metabox with extra field save data

 // Hook into the 'add_meta_boxes' action

add_action('add_meta_boxes', 'custom_post_type_tags_as_checkboxes', 10, 2);


function custom_post_type_tags_as_checkboxes($post_type, $post) {

    if ($post_type == 'properties') { // Change 'your_custom_post_type' to your custom post type slug

        remove_meta_box('tagsdiv-es_tag', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_amenity', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_feature', $post_type, 'side'); // Remove the default tags metabox     

remove_meta_box('tagsdiv-es_neighborhood', $post_type, 'side'); // Remove the default tags metabox

remove_meta_box('tagsdiv-es_category', $post_type, 'side'); // Remove the default tags metabox

        add_meta_box('tagsdiv-post_tag', __('Tags'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'post_tag'));

// Add custom checkboxes for Amenity taxonomy

        add_meta_box('tagsdiv-es_amenity', __('Amenity'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_amenity'));


// Add custom checkboxes for Features taxonomy

        add_meta_box('tagsdiv-es_feature', __('Feature'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_feature'));

// Add custom checkboxes for es_neighborhood taxonomy

        add_meta_box('tagsdiv-es_neighborhood', __('Neighborhoods'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_neighborhood'));


// Add custom checkboxes for es_category taxonomy

        add_meta_box('tagsdiv-es_category', __('Categories'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_category'));

    }

}


// Render checkboxes instead of the default tag input

// Render checkboxes with an option to add new terms

function render_tags_as_checkboxes($post, $box) {

    $taxonomy = $box['args']['taxonomy'];

    $terms = get_terms(array(

        'taxonomy' => $taxonomy,

        'hide_empty' => false,

    ));


    $post_terms = wp_get_post_terms($post->ID, $taxonomy, array('fields' => 'ids'));


    if (!empty($terms)) {

        echo '<div style="max-height: 150px; overflow-y: auto; border: 1px solid #ddd; padding: 5px;">';

        foreach ($terms as $term) {

            $checked = in_array($term->term_id, $post_terms) ? ' checked="checked"' : '';

            echo '<label style="display: block; margin: 3px 0;"><input type="checkbox" name="tax_input[' . $taxonomy . '][]" value="' . $term->term_id . '"' . $checked . '> ' . esc_html($term->name) . '</label>';

        }

        echo '</div>';

    }

    

    // Add new term field

    echo '<div style="margin-top: 10px;">

            <input type="text" id="new_' . $taxonomy . '_term" placeholder="Add new term" style="width: 70%;"/>

            <button type="button" class="button add-term" data-taxonomy="' . $taxonomy . '">Add</button>

          </div>';


    // JavaScript for AJAX request

    echo "<script>

        jQuery(document).ready(function($) {

            $('.add-term').click(function() {

                var taxonomy = $(this).data('taxonomy');

                var newTerm = $('#new_' + taxonomy + '_term').val();


                if (newTerm) {

                    $.ajax({

                        url: ajaxurl,

                        type: 'POST',

                        data: {

                            action: 'add_new_term',

                            taxonomy: taxonomy,

                            term_name: newTerm

                        },

                        success: function(response) {

                            if (response.success) {

                                location.reload(); // Reload to see the new term

                            } else {

                                //alert('Error adding term: ' + response.data);

                            }

                        }

                    });

                }

            });

        });

    </script>";

}



// Save the selected terms when the post is saved

add_action('save_post', 'save_custom_tag_checkboxes');


function save_custom_tag_checkboxes($post_id) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {

        return;

    }


    if (isset($_POST['tax_input']['post_tag'])) {

        $tag_ids = array_map('intval', $_POST['tax_input']['post_tag']);

        wp_set_post_terms($post_id, $tag_ids, 'post_tag');

    } else {

        wp_set_post_terms($post_id, array(), 'post_tag'); // Clear tags if none are selected

    }

    // Save Amenity

    if (isset($_POST['tax_input']['es_amenity'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_amenity']);

        wp_set_post_terms($post_id, $feature_ids, 'es_amenity');

    } else {

        wp_set_post_terms($post_id, array(), 'es_amenity'); // Clear features if none are selected

    }

 

// Save Features

    if (isset($_POST['tax_input']['es_feature'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_feature']);

        wp_set_post_terms($post_id, $feature_ids, 'es_feature');

    } else {

        wp_set_post_terms($post_id, array(), 'es_feature'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_neighborhood'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_neighborhood']);

        wp_set_post_terms($post_id, $feature_ids, 'es_neighborhood');

    } else {

        wp_set_post_terms($post_id, array(), 'es_neighborhood'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_category'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_category']);

        wp_set_post_terms($post_id, $feature_ids, 'es_category');

    } else {

        wp_set_post_terms($post_id, array(), 'es_category'); // Clear features if none are selected

    }

}


add_action('wp_ajax_add_new_term', 'add_new_term');


function add_new_term() {

    if (!isset($_POST['taxonomy']) || !isset($_POST['term_name'])) {

        wp_send_json_error('Invalid request');

    }


    $taxonomy = sanitize_text_field($_POST['taxonomy']);

    $term_name = sanitize_text_field($_POST['term_name']);


    // Insert the term

    $new_term = wp_insert_term($term_name, $taxonomy);


    if (is_wp_error($new_term)) {

        wp_send_json_error($new_term->get_error_message());

    } else {

        wp_send_json_success($new_term);

    }

}


Convert meta box in checkbox in wordpress

 // Hook into the 'add_meta_boxes' action

add_action('add_meta_boxes', 'custom_post_type_tags_as_checkboxes', 10, 2);


function custom_post_type_tags_as_checkboxes($post_type, $post) {

    if ($post_type == 'properties') { // Change 'your_custom_post_type' to your custom post type slug

        remove_meta_box('tagsdiv-es_tag', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_amenity', $post_type, 'side'); // Remove the default tags metabox

        remove_meta_box('tagsdiv-es_feature', $post_type, 'side'); // Remove the default tags metabox     

remove_meta_box('tagsdiv-es_neighborhood', $post_type, 'side'); // Remove the default tags metabox

remove_meta_box('tagsdiv-es_category', $post_type, 'side'); // Remove the default tags metabox

        add_meta_box('tagsdiv-post_tag', __('Tags'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'post_tag'));

// Add custom checkboxes for Amenity taxonomy

        add_meta_box('tagsdiv-es_amenity', __('Amenity'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_amenity'));


// Add custom checkboxes for Features taxonomy

        add_meta_box('tagsdiv-es_feature', __('Feature'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_feature'));

// Add custom checkboxes for es_neighborhood taxonomy

        add_meta_box('tagsdiv-es_neighborhood', __('Neighborhoods'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_neighborhood'));


// Add custom checkboxes for es_category taxonomy

        add_meta_box('tagsdiv-es_category', __('Categories'), 'render_tags_as_checkboxes', $post_type, 'side', 'default', array('taxonomy' => 'es_category'));

    }

}


// Render checkboxes instead of the default tag input

// Render checkboxes with an option to add new terms

function render_tags_as_checkboxes($post, $box) {

    $taxonomy = $box['args']['taxonomy'];

    $terms = get_terms(array(

        'taxonomy' => $taxonomy,

        'hide_empty' => false,

    ));


    $post_terms = wp_get_post_terms($post->ID, $taxonomy, array('fields' => 'ids'));


    if (!empty($terms)) {

        echo '<div style="max-height: 150px; overflow-y: auto; border: 1px solid #ddd; padding: 5px;">';

        foreach ($terms as $term) {

            $checked = in_array($term->term_id, $post_terms) ? ' checked="checked"' : '';

            echo '<label style="display: block; margin: 3px 0;"><input type="checkbox" name="tax_input[' . $taxonomy . '][]" value="' . $term->term_id . '"' . $checked . '> ' . esc_html($term->name) . '</label>';

        }

        echo '</div>';

    }

}



// Save the selected terms when the post is saved

add_action('save_post', 'save_custom_tag_checkboxes');


function save_custom_tag_checkboxes($post_id) {

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {

        return;

    }


    if (isset($_POST['tax_input']['post_tag'])) {

        $tag_ids = array_map('intval', $_POST['tax_input']['post_tag']);

        wp_set_post_terms($post_id, $tag_ids, 'post_tag');

    } else {

        wp_set_post_terms($post_id, array(), 'post_tag'); // Clear tags if none are selected

    }

    // Save Amenity

    if (isset($_POST['tax_input']['es_amenity'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_amenity']);

        wp_set_post_terms($post_id, $feature_ids, 'es_amenity');

    } else {

        wp_set_post_terms($post_id, array(), 'es_amenity'); // Clear features if none are selected

    }

 

// Save Features

    if (isset($_POST['tax_input']['es_feature'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_feature']);

        wp_set_post_terms($post_id, $feature_ids, 'es_feature');

    } else {

        wp_set_post_terms($post_id, array(), 'es_feature'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_neighborhood'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_neighborhood']);

        wp_set_post_terms($post_id, $feature_ids, 'es_neighborhood');

    } else {

        wp_set_post_terms($post_id, array(), 'es_neighborhood'); // Clear features if none are selected

    }

// Save es_neighborhood

    if (isset($_POST['tax_input']['es_category'])) {

        $feature_ids = array_map('intval', $_POST['tax_input']['es_category']);

        wp_set_post_terms($post_id, $feature_ids, 'es_category');

    } else {

        wp_set_post_terms($post_id, array(), 'es_category'); // Clear features if none are selected

    }

}

Thursday 26 September 2024

Create testimonials custom post type in wordpress

function custom_testimonial_post_type() {

    $labels = array(

        'name'               => 'Testimonials',

        'singular_name'      => 'Testimonial',

        'menu_name'          => 'Testimonials',

        'name_admin_bar'     => 'Testimonial',

        'add_new'            => 'Add New',

        'add_new_item'       => 'Add New Testimonial',

        'new_item'           => 'New Testimonial',

        'edit_item'          => 'Edit Testimonial',

        'view_item'          => 'View Testimonial',

        'all_items'          => 'All Testimonials',

        'search_items'       => 'Search Testimonials',

        'not_found'          => 'No Testimonials found.',

        'not_found_in_trash' => 'No Testimonials found in Trash.',

    );

    

    $args = array(

        'labels'             => $labels,

        'public'             => true,

        'publicly_queryable' => true,

        'show_ui'            => true,

        'show_in_menu'       => true,

        'query_var'          => true,

        'rewrite'            => array( 'slug' => 'testimonials' ),

        'capability_type'    => 'post',

        'has_archive'        => true,

        'hierarchical'       => false,

        'menu_position'      => null,

        'supports'           => array( 'title', 'editor', 'thumbnail' ),

    );

    

    register_post_type( 'testimonial', $args );

}

add_action( 'init', 'custom_testimonial_post_type' );


add_shortcode( 'testimonials', 'wpdocs_testimonials_func' ); 


function wpdocs_testimonials_func( $atts ) {

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    $args = array(

        'post_type' => 'testimonial',

        'posts_per_page' => 5, // Number of testimonials per page

        'paged' => $paged

    );

    $testimonial_query = new WP_Query( $args );


    if ( $testimonial_query->have_posts() ) :

        while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post();


$image_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); 

     

 endwhile;


        // Pagination

        $total_pages = $testimonial_query->max_num_pages;

$total_posts = $testimonial_query->found_posts; 

$current_post_count = $testimonial_query->post_count; 

        if ( $total_pages > 1 ) {

echo '<div class="paginationcustom manjeet">';

            $current_page = max( 1, get_query_var( 'paged' ) );

    echo '<ul class="custom-pagination">';


    // Generate pagination links

    $pagination_links = paginate_links( array(

        'base'      => get_pagenum_link( 1 ) . '%_%',

        'format'    => 'page/%#%',

        'current'   => $current_page,

        'total'     => $total_pages,

        'prev_text' => '‹', // Previous arrow

        'next_text' => '›', // Next arrow

        'type'      => 'array', // Output as array to customize

    ));


    // Loop through the links and add custom class

    foreach ( $pagination_links as $link ) {

        if ( strpos( $link, 'current' ) !== false ) {

            echo '<li class="active">' . $link . '</li>'; // Add active class

        } else {

            echo '<li>' . $link . '</li>';

        }

    }


    echo '</ul>';

    echo '<div class="results-count">';

    echo 'Showing ' . $current_post_count . ' of ' . $total_posts . ' results';

    echo '</div>';

echo '</div>';

        }


        wp_reset_postdata();

    else :

        echo '<p>no.testimonial found</p>';

    endif;

    ?>

  </div>

</div>

<?php }


How can i put "posted x minutes ago on my posts?

 Just place this code in your functions.php

function time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';

    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');

}

To use it anywhere in your theme (for example, on single.php), place this code where you want the "time ago" feature displayed:

<?php echo time_ago(); ?>