Related
After my function.php code was modified I get a white screen when trying to go to my wordpress /wp-admin page. What could be causing the issue? Also general code improvement are welcome, as I never coded in php or wordpress before.
function.php file
<?php
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'footer-menu1' => __( 'Footer Menu 1' ),
'footer-menu2' => __( 'Footer Menu 2' ),
'footer-menu3' => __( 'Footer Menu 3' ),
'side-menu' => __( 'Side Menu' ),
)
);
}
add_action( 'init', 'register_my_menus' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'custom-logo' );
add_filter ( 'nav_menu_css_class', 'so_37823371_menu_item_class', 10, 4 );
function so_37823371_menu_item_class ( $classes, $item, $args, $depth ){
$classes[] = 'nav-item';
return $classes;
}
add_filter( 'nav_menu_link_attributes', function($atts) {
$atts['class'] = "nav-link";
return $atts;
}, 100, 1 );
function my_theme_assets_files() {
wp_enqueue_script( 'custom-js', get_template_directory_uri(). '/js/custom.js' , [], '1.0.0' , true );
wp_localize_script( 'custom-js', 'jsh_ajax', [
'ajax_url' => admin_url( 'admin-ajax.php' )
]);
}
add_action( 'wp_enqueue_scripts', 'my_theme_assets_files' );
add_action("wp_ajax_sk_ajax_posts", "sk_ajax_posts");
add_action("wp_ajax_nopriv_sk_ajax_posts", "sk_ajax_posts");
function sk_ajax_posts() {
$cat_id = $_POST['cat_id'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'cat' => $cat_id ,
// 'orderby’ => 'title',
// 'order’ => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
//the_post();
$categories = get_the_category();
$category_name = $categories[0]->name;
?>
<div class="col-md-4 ">
<div class="card border-0">
<div class="image-parent">
<?php echo wp_get_attachment_image( get_post_thumbnail_id( get_the_ID() ), 'full', '', [ 'class' => 'card-img-top' ] ); ?>
<?php echo $category_name; ?>
</div>
<div class="card-body">
<!-- <h6>April 25, 2022</h6> -->
<h5 class="card-title"><?php the_title(); ?></h5>
<p class="card-text"><?php the_excerpt()?></p>
Read the article
</div>
</div>
</div>
<?php
endwhile;
echo paginate_links(array(
'total' => $loop->max_num_pages,
'prev_text' => __('Previous'),
'next_text' => __('Next')
)) . "</div>";
wp_reset_postdata();
die();
}
/*
* Creating a function to create our CPT
*/
function fisher_custom_post_type_advisor() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Advisor\'s', 'Post Type General Name', 'twentytwentyone' ),
'singular_name' => _x( 'advisor', 'Post Type Singular Name', 'twentytwentyone' ),
'menu_name' => __( 'Advisor\'s', 'twentytwentyone' ),
'parent_item_colon' => __( 'advisor', 'twentytwentyone' ),
'all_items' => __( 'All Advisor\'s', 'twentytwentyone' ),
'view_item' => __( 'View Advisor', 'twentytwentyone' ),
'add_new_item' => __( 'Add New Advisor', 'twentytwentyone' ),
'add_new' => __( 'Add New', 'twentytwentyone' ),
'edit_item' => __( 'Edit Advisor', 'twentytwentyone' ),
'update_item' => __( 'Update Advisor', 'twentytwentyone' ),
'search_items' => __( 'Search Advisor', 'twentytwentyone' ),
'not_found' => __( 'Not Found', 'twentytwentyone' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Advisor\'s', 'twentytwentyone' ),
'description' => __( 'Advisor\' news and reviews', 'twentytwentyone' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'advisors' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => false,
);
// Registering your Custom Post Type
register_post_type( 'all-advisors', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'fisher_custom_post_type_advisor', 0 );
function global_notice_meta_box() {
add_meta_box(
'advisor-first-name',
__( 'Advisor Detail', 'sitepoint' ),
'global_notice_meta_box_callback',
'all-advisors'
);
}
add_action( 'add_meta_boxes', 'global_notice_meta_box' );
function global_notice_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'fisher_advisor', 'fisher_advisor' );
global $fields;
$fields = [
array (
'key' => 'first_name',
'label'=> __( 'Full Name'),
'type' => 'text'
),
array (
'key' => 'role',
'label'=> __( 'Title'),
'type' => 'text'
),
array (
'key' => 'professional_certificate_short_form',
'label'=> __( 'Professional Certifications (Short)'),
'type' => 'text'
),
array (
'key' => 'professional_certificate',
'label'=> __( 'Professional Certifications'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'Full Address'),
'type' => 'text'
),
array (
'key' => 'city',
'label' => __( 'Location'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'email',
'label' => __( 'Email' ),
'type' => 'email'
)
];
foreach ( $fields as $field ) {
$value = get_post_meta( $post->ID, '_advisor_'. $field['key'], true );
if ( $field['key'] == 'city' ) {
$args = [ 'post_type' => 'all-locations', 'posts_per_page' => -1 ];
$locations = new WP_Query($args);
if ( $locations->have_posts() ) {
echo '<label><span>' . $field['label'] .'</span>';
echo '<select name="advisor_' . $field['key'] . '">';
while ( $locations->have_posts() ) {
$locations->the_post();
echo '<option value="' . get_the_title() . '">' . get_the_title() . '</option>';
}
echo '</select></label>';
}
} else {
$value = get_post_meta( $post->ID, '_advisor_'. $field['key'], true );
echo '<label><span>' . $field['label'] .'</span> <input type="'. $field['type'] .'" name="advisor_' . $field['key'] . '" value="' . esc_attr( $value ) . '" /></label>';
echo '<br>';
}
}
}
function save_global_notice_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['fisher_advisor'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['fisher_advisor'], 'fisher_advisor' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'all-advisors' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
$fields = [
array (
'key' => 'first_name',
'label'=> __( 'Full Name'),
'type' => 'text'
),
array (
'key' => 'role',
'label'=> __( 'Title'),
'type' => 'text'
),
array (
'key' => 'professional_certificate_short_form',
'label'=> __( 'Professional Certifications (Short)'),
'type' => 'text'
),
array (
'key' => 'professional_certificate',
'label'=> __( 'Professional Certifications'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'Full Address'),
'type' => 'text'
),
array (
'key' => 'city',
'label' => __( 'Location'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'email',
'label' => __( 'Email' ),
'type' => 'email'
)
];
foreach ( $fields as $field ) {
if ( ! isset( $_POST['advisor_' . $field['key']] ) ) return;
$sav_field = sanitize_text_field( $_POST['advisor_' . $field['key']] );
update_post_meta( $post_id, '_advisor_' . $field['key'], $sav_field );
}
}
add_action( 'save_post', 'save_global_notice_meta_box_data' );
/*****cpt for location ***/
/*
* Creating a function to create our CPT
*/
function fisher_custom_post_type_location() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Location\'s', 'Post Type General Name', 'twentytwentyone' ),
'singular_name' => _x( 'Location', 'Post Type Singular Name', 'twentytwentyone' ),
'menu_name' => __( 'Location\'s', 'twentytwentyone' ),
'parent_item_colon' => __( 'Location', 'twentytwentyone' ),
'all_items' => __( 'All Location\'s', 'twentytwentyone' ),
'view_item' => __( 'View Location', 'twentytwentyone' ),
'add_new_item' => __( 'Add New Location', 'twentytwentyone' ),
'add_new' => __( 'Add New', 'twentytwentyone' ),
'edit_item' => __( 'Edit Location', 'twentytwentyone' ),
'update_item' => __( 'Update Location', 'twentytwentyone' ),
'search_items' => __( 'Search Location', 'twentytwentyone' ),
'not_found' => __( 'Not Found', 'twentytwentyone' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'Location\'s', 'twentytwentyone' ),
'description' => __( 'Location\' news and reviews', 'twentytwentyone' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' ),
// You can associate this CPT with a taxonomy or custom taxonomy.
// 'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'locations' ),
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => false,
);
// Registering your Custom Post Type
register_post_type( 'all-locations', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'fisher_custom_post_type_location', 0 );
/****locatin-meta-box**** */
function global_notice_meta_box_location() {
add_meta_box(
'advisor-first-name',
__( 'Location Detail', 'sitepoint' ),
'global_notice_meta_box_location_callback',
'all-locations'
);
}
add_action( 'add_meta_boxes', 'global_notice_meta_box_location' );
function global_notice_meta_box_location_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'fisher_location', 'fisher_location' );
global $fields;
$fields = [
array (
'key' => 'city',
'label'=> __( 'city'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'adress'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'lati',
'label' => __( 'latitutde' ),
'type' => 'text'
),
array (
'key' => 'langi',
'label' => __( 'langitude' ),
'type' => 'text'
),
];
foreach ( $fields as $field ) {
$value = get_post_meta( $post->ID, '_location_'. $field['key'], true );
echo '<label><span>' . $field['label'] .'</span> <input type="'. $field['type'] .'" name="location_' . $field['key'] . '" value="' . esc_attr( $value ) . '" /></label>';
echo '<br>';
}
}
function save_global_notice_meta_box_location_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['fisher_location'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['fisher_location'], 'fisher_location' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'all-locations' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
$fields = [
array (
'key' => 'city',
'label'=> __( 'city'),
'type' => 'text'
),
array (
'key' => 'adress',
'label'=> __( 'adress'),
'type' => 'text'
),
array (
'key' => 'phone',
'label' => __( 'Phone' ),
'type' => 'tel'
),
array (
'key' => 'lati',
'label' => __( 'latitutde' ),
'type' => 'text'
),
array (
'key' => 'langi',
'label' => __( 'langitude' ),
'type' => 'text'
),
];
foreach ( $fields as $field ) {
if ( ! isset( $_POST['location_' . $field['key']] ) ) return;
$sav_field = sanitize_text_field( $_POST['location_' . $field['key']] );
update_post_meta( $post_id, '_location_' . $field['key'], $sav_field );
}
}
When the below was added, I started getting a blank screen for wordpress when I go to /wp-admin
add_action( 'save_post', 'save_global_notice_meta_box_location_data' );
?>
<?php
//SETTING UP THE PAGE
function set_page() {
add_menu_page('Social Media Links', 'Social Media Links', 'manage_options', 'theme_settings', 'the_page', 'dashicons-share', 40);
}
//CALLING THE FUNCTION
add_action('admin_menu', 'set_page');?>
<?php //THE PAGE
function the_page() {?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri()?>/css/bootstrap.min.css">
<legend style="background: #2271b1;color: #fff;padding: 15px 30px;margin-bottom: 60px;margin-top: 10px;padding-right: 85px;max-width: 99%;">Social media linked accounts</legend>
<div class="container"><div class="row"><div class="col-md-12">
<form method="post" action="options.php">
<?php settings_fields('theme_settings'); ?>
<?php $options = get_option('theme_settings'); ?>
<?php if ($_REQUEST['settings-updated']) : ?>
<div class="updated">
<p><strong class="alret">
<?php _e('Options saved'); ?>
</strong></p>
</div>
<?php endif; ?>
<div class="the-container">
<div id="social" class="container tab-pane fade active show"><br>
<fieldset>
<?php $social_arr = ['facebook','twitter','linkedin','youtube'];
foreach($social_arr as $social){?>
<div class="form-group form_builder_row facebook_container row">
<div class="col-md-2 form_builder_col">
<label class="control-label <?php echo $social;?>" for="<?php echo $social;?>">
<?php echo ucfirst($social);?>:</label>
</div>
<div class="col-md-6 form_builder_col">
<input type="text" class=" form_builder_field form-control input-text field_<?php echo $social;?>" id="<?php echo $social;?>" name="theme_settings[<?php echo $social;?>]" value="<?php echo $options[$social];?>" placeholder="<?php echo ucfirst($social);?>">
</div></div>
<?php }?>
</fieldset>
</div>
</div>
<p class="mt-4">
<input name="theme_settings[submit]" id="submit" value="Save Changes" type="submit" class="button button-primary btn-md ml-3">
</p>
</form>
</div></div></div>
<?php } ?>
<?php
//REGISTERING SETTINGS AND FIELDS
function register_settings_and_fields() {
register_setting('theme_settings','theme_settings');
}
add_action('admin_init', 'register_settings_and_fields');
?>
Have you opened wp-config.php? and set WP_DEBUG to true; this can show some errors more intuitively
Don't use function names like this the_page set_page use a prefix before function name e.g.: lefty_wp_set_page , lefty_wp_the_page or give a function a meaningful name, don't just copy paste things from internet, do some modifications in it to avoid conflicts.
I am modifying a wordpress theme 'jobify' . In their demo they have a 'search hero' widget with a button which takes you to a jobs listings page. Inspecting in chrome:
<form class="job_search_form job_search_form--flat"
action="https://jobify-demos.astoundify.com/classic/find-a-job/"
method="GET">
Their 'find-a-job' page which you get taken to with the button has google maps which I don't want. So I have created a new [jobs] page I named 'job-search' without the map. If I edit the above url to that in chrome inspector then the button outputs the search results correctly on my new page. following this
I have now spent 2 days going through all the widget php code trying to find where I change that GET command url on this widget button from 'find-a-job' to 'job-search'. Adjusting this on a newly made search page seems straightforward as seen on this SO question
I have read about customising their widgets here and thought it would be in the class-widget-search-hero.php file (below). I have also checked its scss file but don't see anything there. So many hours looking, is it something as simple as deleting one of the two [jobs] pages? Thank you.
<?php
/**
* Home: Search Hero
*
* #package Jobify
* #category Widget
* #since 3.0.0
*/
class Jobify_Widget_Search_Hero extends Jobify_Widget {
public function __construct() {
$this->widget_description = __( 'Display a "hero" search area.', 'jobify' );
$this->widget_id = 'jobify_widget_search_hero';
$this->widget_cssclass = 'widget--home-hero-search';
$this->widget_name = __( 'Jobify - Page: Search Hero', 'jobify' );
$this->control_ops = array(
'width' => 400,
);
$this->settings = array(
'home widgetized' => array(
'std' => __( 'Homepage/Widgetized', 'jobify' ),
'type' => 'widget-area',
),
'height' => array(
'type' => 'select',
'std' => 'medium',
'label' => __( 'Hero Height', 'jobify' ),
'options' => array(
'small' => __( 'Small', 'jobify' ),
'medium' => __( 'Medium', 'jobify' ),
'large' => __( 'Large', 'jobify' ),
),
),
'margin' => array(
'type' => 'checkbox',
'std' => 1,
'label' => __( 'Add standard spacing above/below widget', 'jobify' ),
),
'text_color' => array(
'type' => 'colorpicker',
'std' => '#ffffff',
'label' => __( 'Text Color:', 'jobify' ),
),
'title' => array(
'type' => 'text',
'std' => '',
'label' => __( 'Title:', 'jobify' ),
),
'description' => array(
'type' => 'text',
'std' => '',
'label' => __( 'Description:', 'jobify' ),
'rows' => 5,
),
'image' => array(
'type' => 'image',
'std' => '',
'label' => __( 'Background Image:', 'jobify' ),
),
'background_position' => array(
'type' => 'select',
'std' => 'center center',
'label' => __( 'Image Position:', 'jobify' ),
'options' => array(
'left top' => __( 'Left Top', 'jobify' ),
'left center' => __( 'Left Center', 'jobify' ),
'left bottom' => __( 'Left Bottom', 'jobify' ),
'right top' => __( 'Right Top', 'jobify' ),
'right center' => __( 'Right Center', 'jobify' ),
'right bottom' => __( 'Right Bottom', 'jobify' ),
'center top' => __( 'Center Top', 'jobify' ),
'center center' => __( 'Center Center', 'jobify' ),
'center bottom' => __( 'Center Bottom', 'jobify' ),
'center top' => __( 'Center Top', 'jobify' ),
),
),
'cover_overlay' => array(
'type' => 'checkbox',
'std' => 1,
'label' => __( 'Use transparent overlay', 'jobify' ),
),
);
if ( jobify()->get( 'wp-job-manager-resumes' ) ) {
$what = array(
'what' => array(
'type' => 'select',
'std' => 'job',
'label' => __( 'Search:', 'jobify' ),
'options' => array(
'job' => __( 'Jobs', 'jobify' ),
'resume' => __( 'Resumes', 'jobify' ),
),
),
);
$this->settings = $what + $this->settings;
}
parent::__construct();
}
function widget( $args, $instance ) {
extract( $args );
$text_align = isset( $instance['text_align'] ) ? esc_attr( $instance['text_align'] ) : 'left';
$background_position = isset( $instance['background_position'] ) ? esc_attr( $instance['background_position'] ) : 'center center';
$overlay = isset( $instance['cover_overlay'] ) && 1 == $instance['cover_overlay'] ? 'has-overlay' : 'no-overlay';
$margin = isset( $instance['margin'] ) && 1 == $instance['margin'] ? true : false;
$height = isset( $instance['height'] ) ? esc_attr( $instance['height'] ) : 'medium';
if ( ! $margin ) {
$before_widget = str_replace( 'widget--home ', 'widget--home widget--home--no-margin ', $before_widget );
}
$image = isset( $instance['image'] ) ? esc_url( $instance['image'] ) : null;
$content = $this->assemble_content( $instance );
$what = isset( $instance['what'] ) ? esc_attr( $instance['what'] ) : 'job';
global $is_flat;
$is_flat = true;
ob_start();
?>
<?php echo $before_widget; ?>
<div class="hero-search hero-search--<?php echo esc_attr( $overlay ); ?> hero-search--height-<?php echo esc_attr( $height ); ?>" style="background-image:url(<?php echo $image; ?>); ?>; background-position: <?php echo $background_position; ?>">
<div class="container">
<?php echo $content; ?>
<?php locate_template( array( $what . '-filters-flat.php' ), true, false ); ?>
</div>
</div>
<?php echo $after_widget; ?>
<?php
$content = ob_get_clean();
echo apply_filters( $this->widget_id, $content );
}
private function assemble_content( $instance ) {
$text_color = isset( $instance['text_color'] ) ? esc_attr( $instance['text_color'] ) : '#fff';
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$content = isset( $instance['description'] ) ? $instance['description'] : '';
$output = '<div class="hero-search__content" style="color:' . $text_color . '">';
$output .= '<h2 class="hero-search__title" style="color:' . $text_color . '">' . $title . '</h2>';
$output .= wpautop( $content );
$output .= '</div>';
return $output;
}
}
The solution (if not technical answer) was actually very easy once found.
After reading this tutorial and seeing the line:
$action_page_id = get_option('job_manager_jobs_page_id');
Then searching "job_manager_jobs_page_id" and finding this on github I discovered it exists under the job manager settings /wp-admin/edit.php?post_type=job_listing&page=job-manager-settings#settings-job_pages where you just change the 'Job Listings' page in the drop down from 'find a job' to my new 'job search' page. All working.
I already think I know the problem, there is a <?php somewhere or somewhere there is written <? and it makes the site crash but I don't know where it is.
Now I don't ask you to look through the whole code but if you know where I would find this if this ever happened to you.
So I migrated the site from cpanel to a new self managed server. (runcloud "on top off" digital ocean)
But after the migration it shows this kind of output.
Thanks for taking your time to answer this question.
I already searched for the same question but I don't seem to find the same problem.
`flush_rules(); //regestering menu register_nav_menus(array( 'top_menu'=>'Top navigation' ,'main_menu' => 'Main navigation' ,'inner_menu' => 'Inner navigation')); //add thumnails support if (function_exists('add_theme_support')){ add_theme_support('post-thumbnails'); } //disable auto image link function setup_default_image_link() { $original_setting = get_option( 'image_default_link'); if ($original_setting !== 'none') { update_option('image_default_link_type', 'none'); } } add_action('admin_init', 'setup_default_image_link', 50); update_option('image_default_link_type', 'none'); //remove P around images function filter_ptags_on_images($content){ return preg_replace('/
\s*()?\s*()\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); } add_filter('the_content', 'filter_ptags_on_images'); //add support of page attributes add_post_type_support( 'post', 'page-attributes' ); /* * Switch default core markup for search form, comment form, and comments * to output valid HTML5. */ add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ) ); //add custom header with image or video /* //image add_theme_support( 'custom-header' ); //video add_theme_support( 'custom-header', array( 'video' => true, ) ); /**/ //add_theme_support( 'selective-refresh' ); add_theme_support( 'customize-selective-refresh-widgets' ); /* auto-detect the server so you only have to enter the front/from half of the email address, including the # sign */ /** function xyz_filter_wp_mail_from($email){ $sitename = strtolower( $_SERVER['SERVER_NAME'] ); if ( substr( $sitename, 0, 4 ) == 'www.' ) { $sitename = substr( $sitename, 4 ); } $myfront = "noreply#"; $myback = $sitename; $myfrom = $myfront . $myback; return $myfrom; } add_filter("wp_mail_from", "xyz_filter_wp_mail_from"); function xyz_filter_wp_mail_from_name($from_name){ $sitename = strtolower( $_SERVER['SERVER_NAME'] ); if ( substr( $sitename, 0, 4 ) == 'www.' ) { $sitename = substr( $sitename, 4 ); } return $sitename; } add_filter("wp_mail_from_name", "xyz_filter_wp_mail_from_name"); /**/ //remove footer add_filter( 'admin_footer_text', '__return_empty_string', 11 ); add_filter( 'update_footer', '__return_empty_string', 11 ); //remove Category, Archive, etc. from title add_filter( 'get_the_archive_title', function ($title) { if ( is_category() ) { $title = sprintf( __( '%s' ), single_cat_title( '', false ) ); } elseif ( is_tag() ) { $title = sprintf( __( '%s' ), single_tag_title( '', false ) ); } elseif ( is_author() ) { $title = sprintf( __( '%s' ), '' . get_the_author() . '' ); } elseif ( is_year() ) { $title = sprintf( __( '%s' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) ); } elseif ( is_month() ) { $title = sprintf( __( '%s' ), get_the_date( _x( 'F Y', 'monthly archives date format' ) ) ); } elseif ( is_day() ) { $title = sprintf( __( '%s' ), get_the_date( _x( 'F j, Y', 'daily archives date format' ) ) ); } elseif ( is_tax( 'post_format' ) ) { if ( is_tax( 'post_format', 'post-format-aside' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-image' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-video' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-quote' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-link' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-status' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-audio' ) ) { $title = _x( '', 'post format archive title' ); } elseif ( is_tax( 'post_format', 'post-format-chat' ) ) { $title = _x( '', 'post format archive title' ); } } elseif ( is_post_type_archive() ) { $title = sprintf( __( '%s' ), post_type_archive_title( '', false ) ); } elseif ( is_tax() ) { $tax = get_taxonomy( get_queried_object()->taxonomy ); $title = sprintf( __( '%1$s: %2$s' ), $tax->labels->singular_name, single_term_title( '', false ) ); } else { $title = __( '' ); } return $title; }); //set count of posts on custom archive page function set_posts_per_page_for_towns_cpt( $query ) { if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'news' ) || is_tax( 'news_categories' ) ) { $query->set( 'posts_per_page', '6' ); }elseif ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'our_work' ) ) { $query->set( 'posts_per_page', '6' ); } } add_action( 'pre_get_posts', 'set_posts_per_page_for_towns_cpt' ); function remove_menus(){ remove_menu_page( 'edit.php' ); //Posts http://sw/wp-admin/ // remove_menu_page( 'index.php' ); //Dashboard // remove_menu_page( 'jetpack' ); //Jetpack* // remove_menu_page( 'upload.php' ); //Media // remove_menu_page( 'edit.php?post_type=page' ); //Pages // remove_menu_page( 'edit-comments.php' ); //Comments // remove_menu_page( 'themes.php' ); //Appearance // remove_menu_page( 'plugins.php' ); //Plugins // remove_menu_page( 'users.php' ); //Users // remove_menu_page( 'tools.php' ); //Tools // remove_menu_page( 'options-general.php' ); //Settings } add_action( 'admin_menu', 'remove_menus' ); //add tiny mce templates add_filter( 'tinymce_templates_enable_media_buttons', function(){ return true; }); function change_wp_search_size($query) { if ( $query->is_search){ // Make sure it is a search page $query->query_vars['posts_per_page'] = -1; // Change 10 to the number of posts you would like to show //$query->set('post_type',array('publications')); } return $query; // Return our modified query variables } //add_filter('pre_get_posts', 'change_wp_search_size'); ?> array( 'name' => 'News', 'all_items' => 'All News' ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'news'), 'supports' => array('title'), 'exclude_from_search' => false )); register_post_type('events', array( 'labels' => array( 'name' => 'Events', 'all_items' => 'All Events' ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'events'), 'supports' => array('title'), 'exclude_from_search' => false )); register_post_type('publications', array( 'labels' => array( 'name' => 'Publications', 'all_items' => 'All Publications' ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'publications'), 'supports' => array('title'), 'exclude_from_search' => false )); register_post_type('our_work', array( 'labels' => array( 'name' => 'Our Work', 'all_items' => 'All Work' ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'our-work'), 'supports' => array('title'), 'exclude_from_search' => false )); register_post_type('members', array( 'labels' => array( 'name' => 'Our Members', 'all_items' => 'All Members' ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'members'), 'supports' => array('title'), 'exclude_from_search' => false )); } add_action('init', 'create_post_types'); // register Taxonomies function create_taxonomies() { register_taxonomy('news_categories', array('news'), array( 'labels' => array( 'name' => 'News Categories' ), 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'news' ) )); register_taxonomy('publications_categories', array('publications'), array( 'labels' => array( 'name' => 'Publications Categories' ), 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'publications' ) )); register_taxonomy('members_categories', array('members'), array( 'labels' => array( 'name' => 'Members Categories' ), 'show_ui' => true, 'show_tagcloud' => false, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'members' ) )); } add_action('init', 'create_taxonomies'); // rewrite urls function taxonomy_slug_rewrite($wp_rewrite) { $rules = array(); $taxonomies = get_taxonomies(array('_builtin' => false), 'objects'); $post_types = get_post_types(array('public' => true, '_builtin' => false), 'names'); foreach ($post_types as $post_type) { foreach ($taxonomies as $taxonomy) { if ($taxonomy->object_type[0] == $post_type) { $categories = get_categories(array('type' => $post_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0)); foreach ($categories as $category) { $rules[$post_type . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug; } } } } $wp_rewrite->rules = $rules + $wp_rewrite->rules; } add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' ); ?> $user_id, 'admin_color' => 'custom' ); wp_update_user( $args ); } add_action('user_register', 'set_default_admin_color'); if(get_current_user_id() != 1){ remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' ); } ?> admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('myajax-nonce'))); //wp_localize_script('plugins', 'WPURLS', array( 'siteurl' => get_bloginfo("template_url") )); //wp_register_script( 'retina', get_template_directory_uri() . '/assets/js/libs/retina.min.js', array( 'backbone' )); wp_enqueue_script( 'custom-script' ); wp_enqueue_script('detect'); wp_enqueue_script('main_scripts'); wp_enqueue_script('plugins'); // wp_enqueue_script('retina'); } add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' ); //load admin scripts add_action( 'admin_enqueue_scripts', 'load_admin_scripts' ); function load_admin_scripts() { wp_enqueue_script( 'admin_js', get_admin_url() . 'backend/current/js/scripts.js' ); wp_localize_script( 'admin_js', 'backend_path', get_admin_url()); } if (!is_admin()) { //move all scripts to the footer function footer_enqueue_scripts(){ remove_action('wp_head','wp_print_scripts'); remove_action('wp_head','wp_print_head_scripts',9); remove_action('wp_head','wp_enqueue_scripts',1); add_action('wp_footer','wp_print_scripts',5); add_action('wp_footer','wp_enqueue_scripts',5); add_action('wp_footer','wp_print_head_scripts',5); } add_action('after_setup_theme','footer_enqueue_scripts'); } ?> __( 'Footer Main Menu', 'CURRENT WEBSITE' ), // 'id' => 'main_menu_widget', // 'description' => __( '', 'CURRENT WEBSITE' ), // 'before_widget' => '', // 'before_title' => '
', // 'after_title' => '
', // 'after_widget' => '
', // ) ); register_sidebar( array( 'name' => __( 'Facebook', 'CURRENT WEBSITE' ), 'id' => 'facebook_widget', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'before_title' => '
', 'after_title' => '
', 'after_widget' => '
', ) ); register_sidebar( array( 'name' => __( 'Twitter', 'CURRENT WEBSITE' ), 'id' => 'twitter_widget', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '', 'before_title' => '
', 'after_title' => '
', 'after_widget' => '
', ) ); register_sidebar( array( 'name' => __( 'Main Section Footer', 'CURRENT WEBSITE' ), 'id' => 'main_section_footer', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); /* register_sidebar( array( 'name' => __( 'Language Switcher', 'CURRENT WEBSITE' ), 'id' => 'language_switcher', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __( 'Main Content Right Sidebar', 'CURRENT WEBSITE' ), 'id' => 'main_content_right_sidebar', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __( 'Main Content Left Sidebar', 'CURRENT WEBSITE' ), 'id' => 'main_content_left_sidebar', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __( 'Footer Right Sidebar', 'CURRENT WEBSITE' ), 'id' => 'footer_right_sidebar', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); register_sidebar( array( 'name' => __( 'Footer Center Content', 'CURRENT WEBSITE' ), 'id' => 'footer_center_content', 'description' => __( '', 'CURRENT WEBSITE' ), 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '
', 'after_title' => '
', ) ); */ } add_action( 'widgets_init', 'my_widgets_init' ); ?>`
If I understood the question correctly, you have some errors in your code but you don't know where exactly those errors are.
In this case you should either:
check your system PHP error log
or turn on WP_DEBUG in your wp-config.php file (see WP docs)
You'll see errors/warnings/notices produced by the PHP engine, and those messages will contain something like ... on line XXX in /some/path/to/some/file.php. This tells you exactly where the error/warning/notice happens. Open that file on that line and fix the issue.
I've created a user role of CSR and several custom checkout fields to appear on the Checkout page of WooCommerce, and I want to hide these checkout fields from any other user but those with the CSR role.
I've created the fields and the role, but something is off with my fields as they're still showing up for all users. I followed the tutorial here to hide the fields. Apologies if the formatting of the code is off. The editor didn't accept most of my formatting when I pulled it in from Atom.
ADD CUSTOM FIELDS
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div class="my_custom_checkout_field"><h2>' . __('CSR Information')
.'</h2>';
woocommerce_form_field( 'date_of_purchase', array(
'type' => 'text',
'label' => __('Date of Purchase', 'woocommerce'),
'placeholder' => _x('MM/DD/YYYY', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'date_of_purchase' ));
woocommerce_form_field( 'place_of_purchase', array(
'type' => 'select',
'label' => __('Place of Purchase', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'place_of_purchase' ));
woocommerce_form_field( 'color_item', array(
'type' => 'select',
'label' => __('Product Color', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'color_item' ));
woocommerce_form_field( 'product_model', array(
'type' => 'select',
'label' => __('Model', 'woocommerce'),
'placeholder' => _x('Select Option', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
), $checkout->get_value( 'product_model' ));
echo '<strong>' . __('Check All That Apply:') .'</strong>';
woocommerce_form_field( 'lightbulb_out', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Lightbulb is Out'),
'required' => false,
), $checkout->get_value( 'lightbulb_out' ));
woocommerce_form_field( 'not_turn_on', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Will Not Turn On'),
'required' => false,
), $checkout->get_value( 'not_turn_on' ));
woocommerce_form_field( 'fan_not_running', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Fan Stopped Running'),
'required' => false,
), $checkout->get_value( 'fan_not_running' ));
woocommerce_form_field( 'strange_noise', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Strange Noise'),
'required' => false,
), $checkout->get_value( 'strange_noise' ));
woocommerce_form_field( 'not_catching', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Not Catching Insects'),
'required' => false,
), $checkout->get_value( 'not_catching' ));
woocommerce_form_field( 'csr_other', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Other'),
'required' => false,
), $checkout->get_value( 'csr_other' ));
woocommerce_form_field( 'case_description', array(
'type' => 'textarea',
'label' => __('Description of Case', 'woocommerce'),
'placeholder' => _x('Please provide details', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
), $checkout->get_value( 'case_description' ));
echo '</div>';
}
ADD CSR ROLE
$result = add_role( 'csr', __('CSR' ),
array(
'read' => true, // true allows this capability
'edit_posts' => false, // Denies user to edit their own posts
'edit_pages' => false, // Denies user to edit pages
'edit_others_posts' => false, // Denies user to edit others posts not just
their own
'create_posts' => false, // Denies user to create new posts
'manage_categories' => false, // Denies user to manage post categories
'publish_posts' => false, // Denies the user to publish, otherwise posts stays
in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your
theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
);
HIDE CSR VALUES FOR ALL BUT CSR
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'csr' ) && isset( $fields['date_of_purchase'] ) ) {
unset( $fields[['date_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['place_of_purchase'] ) ) {
unset( $fields[['place_of_purchase']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['color_item'] ) ) {
unset( $fields[['color_item']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['product_model'] ) ) {
unset( $fields[['product_model']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['lightbulb_out'] ) ) {
unset( $fields[['lightbulb_out']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_turn_on'] ) ) {
unset( $fields[['not_turn_on']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['fan_not_running'] ) ) {
unset( $fields[['fan_not_running']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['strange_noise'] ) ) {
unset( $fields[['strange_noise']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['not_catching'] ) ) {
unset( $fields[['not_catching']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['csr_other'] ) ) {
unset( $fields[['csr_other']] );
}
if ( ! current_user_can( 'csr' ) && isset( $fields['case_description'] ) ) {
unset( $fields[['case_description']] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields'
);
I've expanded on the code I posted in my tutorial on how to customize the WooCommerce Checkout.
First you need to register the new checkout fields. This is where I've added a current_user_can() to test if the current user has the appropriate capabilities to see these extra fields. You can probably use current_user_can( 'csr' ) or even better add something like a manage_csr capability to the csr role. I'm using the manage_options capability because it was easier for me to test.
// Add new checkout fields
function kia_filter_checkout_fields( $fields ){
if( current_user_can( 'manage_options' ) ) {
$fields['extra_fields'] = array(
'date_of_purchase' => array(
'type' => 'text',
'label' => __( 'Date of Purchase', 'your-plugin' ),
'placeholder' => _x ('MM/DD/YYYY', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true
),
'place_of_purchase' => array(
'type' => 'select',
'label' => __( 'Place of Purchase', 'your-plugin' ),
'placeholder' => _x( 'Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array('form-row-wide' ),
'clear' => true,
'options' => array('option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'color_item' => array(
'type' => 'select',
'label' => __( 'Product Color', 'your-plugin' ),
'placeholder' => _x( 'Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'product_model' => array(
'type' => 'select',
'label' => __( 'Model', 'your-plugin' ),
'placeholder' => _x('Select Option', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true,
'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
),
'product_condition' => array(
'type' => 'multicheck',
'label' => __( 'Product Condition:', 'your-plugin' ),
'description' => __( 'Check All That Apply:', 'your-plugin' ),
'required' => false,
'clear' => true,
'options' => array( 'lightbulb_out' => __( 'Lightbulb is Out', 'your-plugin' ),
'not_turn_on' => __( 'Will Not Turn On', 'your-plugin' ),
'fan_not_running' => __( 'Fan Stopped Running', 'your-plugin' ),
'strange_noise' => __( 'Strange Noise', 'your-plugin' ),
'not_catching' => __( 'Not Catching Insectsn', 'your-plugin' ),
'csr_other' => __( 'Other', 'your-plugin' ),
),
),
'case_description' => array(
'type' => 'textarea',
'label' => __( 'Description of Case', 'your-plugin' ),
'placeholder' => _x( 'Please provide details', 'placeholder', 'your-plugin' ),
'required' => false,
'class' => array( 'form-row-wide' ),
'clear' => true,
),
);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
You will note that I've done something different with your "Fan Stopped", etc checkboxes. You don't have to do this, but I was curious and procrastinating. WooCommerce doesn't support a multi-check set of checkboxes, but it does support defining your own custom field types. So with the following we create a new type of form field:
function kia_multicheck_form_field( $field, $key, $args, $value ){
$field_html = '<fieldset>';
if( isset( $args['label'] ) ){
$field_html .= '<legend>' . $args['label'] . '</legend>';
}
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$field_html .= '<input type="checkbox" class="input-multicheck ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '[]" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
$field_html .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="multicheck ' . implode( ' ', $args['label_class'] ) . '">' . $option_text . '</label>';
}
}
if ( $args['description'] ) {
$field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
}
$field_html .= '</fieldset>';
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';
$field_container = '<p class="form-row %1$s" id="%2$s" data-sort="' . esc_attr( $sort ) . '">%3$s</p>';
$field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;
return $field;
}
add_filter( 'woocommerce_form_field_multicheck', 'kia_multicheck_form_field', 10, 4 );
Next we display the new fields on the checkout page.... but only if they exist, because harkening back to the first code block, they won't be in the checkout fields array if the user doesn't have the right permissions.
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout();
if( isset( $checkout->checkout_fields['extra_fields'] ) ) { ?>
<div class="extra-fields">
<h3><?php _e( 'CSR Information' ); ?></h3>
<?php
// because of this foreach, everything added to the array in the previous function will display automagically
foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) :
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
endforeach; ?>
</div>
<?php }
}
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
Now you'll probably need to add some CSS to make this look better, and you need a way to save the multicheck data. You can review my tutorial on how to save the rest, and perhaps figure out the multicheck on your own.
I use woocommerce and the standard widgets are fine, but I want to add the option to show products only from a certain category. Here is the code for the standard widget:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* List products. One widget to rule them all.
*
* #author WooThemes
* #category Widgets
* #package WooCommerce/Widgets
* #version 2.3.0
* #extends WC_Widget
*/
class WC_Widget_Products extends WC_Widget {
/**
* Constructor
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_products';
$this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' );
$this->widget_id = 'woocommerce_products';
$this->widget_name = __( 'WooCommerce Products', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Products', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' )
),
'number' => array(
'type' => 'number',
'step' => 1,
'min' => 1,
'max' => '',
'std' => 5,
'label' => __( 'Number of products to show', 'woocommerce' )
),
'show' => array(
'type' => 'select',
'std' => '',
'label' => __( 'Show', 'woocommerce' ),
'options' => array(
'' => __( 'All Products', 'woocommerce' ),
'featured' => __( 'Featured Products', 'woocommerce' ),
'onsale' => __( 'On-sale Products', 'woocommerce' ),
)
),
'orderby' => array(
'type' => 'select',
'std' => 'date',
'label' => __( 'Order by', 'woocommerce' ),
'options' => array(
'date' => __( 'Date', 'woocommerce' ),
'price' => __( 'Price', 'woocommerce' ),
'rand' => __( 'Random', 'woocommerce' ),
'sales' => __( 'Sales', 'woocommerce' ),
)
),
'order' => array(
'type' => 'select',
'std' => 'desc',
'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
'options' => array(
'asc' => __( 'ASC', 'woocommerce' ),
'desc' => __( 'DESC', 'woocommerce' ),
)
),
'in_cat' => array(
'type' => 'text',
'std' => __( 'Category Number', 'woocommerce' ),
'label' => __( 'In Category', 'woocommerce' )
),
'hide_free' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Hide free products', 'woocommerce' )
),
'show_hidden' => array(
'type' => 'checkbox',
'std' => 0,
'label' => __( 'Show hidden products', 'woocommerce' )
)
);
parent::__construct();
}
/**
* Query the products and return them
* #param array $args
* #param array $instance
* #return WP_Query
*/
public function get_products( $args, $instance ) {
$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
$show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std'];
$orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std'];
$order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std'];
$cat = ! empty( $instance['in_cat'] ) ? sanitize_title( $instance['in_cat'] ) : $this->settings['in_cat']['std'];
$query_args = array(
'posts_per_page' => $number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'post_type=products&cat='.$cat
);
if ( empty( $instance['show_hidden'] ) ) {
$query_args['meta_query'][] = WC()->query->visibility_meta_query();
$query_args['post_parent'] = 0;
}
if ( ! empty( $instance['hide_free'] ) ) {
$query_args['meta_query'][] = array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'DECIMAL',
);
}
$query_args['meta_query'][] = WC()->query->stock_status_meta_query();
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
switch ( $show ) {
case 'featured' :
$query_args['meta_query'][] = array(
'key' => '_featured',
'value' => 'yes'
);
break;
case 'onsale' :
$product_ids_on_sale = wc_get_product_ids_on_sale();
$product_ids_on_sale[] = 0;
$query_args['post__in'] = $product_ids_on_sale;
break;
}
switch ( $orderby ) {
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'rand' :
$query_args['orderby'] = 'rand';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
default :
$query_args['orderby'] = 'date';
}
return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) );
}
/**
* widget function.
*
* #see WP_Widget
*
* #param array $args
* #param array $instance
*/
public function widget( $args, $instance ) {
if ( $this->get_cached_widget( $args ) ) {
return;
}
ob_start();
if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) {
$this->widget_start( $args, $instance );
echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) );
}
echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' );
$this->widget_end( $args );
}
wp_reset_postdata();
echo $this->cache_widget( $args, ob_get_clean() );
}
}
As you can see I've added line 70:
'in_cat' => array(
'type' => 'text',
'std' => __( 'Category Number', 'woocommerce' ),
'label' => __( 'In Category', 'woocommerce' )
and also at Line 101:
$cat = ! empty( $instance['in_cat'] ) ? sanitize_title( $instance['in_cat'] ) : $this->settings['in_cat']['std'];
The option is available in the widget to be able to entera category ID and it saves when I click the save button.
The bit I'm struggling with is actually applying the filter, you will see i've added to Line 110:
'post_type=products&cat='.$cat
and I've also tried:
'category_name' => $cat
But I can't get the filters to work. Could anyone point me in the right direction please?
Cheers
Chris
Product category is custom taxonomy, you need to use 'tax_query' to solve the purpose as follows,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $cat,
),
),
Here, $cat should be string/array of taxonomy terms.
Even you can use, woocommerce shortcode if it suites your requirement,
[product_category category="appliances"]
Here, 'appliances' is slug of product category.