Ben Rothman - WordPress Developer Web Development Archives - Page 3 of 4 - ©2023 Ben Rothman, Web Developer

Blog

My blog is dedicated to sharing my expertise, recommendations, and tutorials on WordPress. If you're interested in developing with WordPress, my blog is a valuable resource that you won't want to miss.

Categories
Project Web Development WordPress

Introducing ChatPress!

Hey everyone! ChatPress is a plugin that I am working on that creates chatrooms on any page that they are put on in a WordPress website. I did not put this in the plugin repository because it is more of a personal project, but it was a lot of fun to create.

The plugin uses asynchronous javascript calls to PHP functions to get every message from the server, separate out the messages for other chatrooms and then post all of those that are left. I definitely recommend checking out this plugin, and it is also on my github if you want to see the code or contribute a new feature!

Categories
Web Development Website WordPress

How to Add a Custom Taxonomy to Users in WordPress

Last Tested: 2/5/2022

This will guide you through setting up a custom taxonomy that you can add to and change whenever you are looking at the profile of a user, it has functions to register, display and save the changes to the taxonomy. In our example we are going to assign each user a department or departments, to later use to give them access to the page or pages for the given department.

/* Step 1 Register the Taxonomy */
Register the taxonomy by placing the function below in your functions.php file or in your plugin file depending on your preference:

function custom_user_taxonomy() {

  $labels = array(
    'name'                       => _x( 'Departments', 'Departments Name', 'text_domain' ),
    'singular_name'              => _x( 'Department', 'Department Name', 'text_domain' ),
    'menu_name'                  => __( 'Departments', 'text_domain' ),
    'all_items'                  => __( 'All Departments', 'text_domain' ),
    'parent_item'                => __( 'Parent Department', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Department:', 'text_domain' ),
    'new_item_name'              => __( 'New Department Name', 'text_domain' ),
    'add_new_item'               => __( 'Add Department', 'text_domain' ),
    'edit_item'                  => __( 'Edit Department', 'text_domain' ),
    'update_item'                => __( 'Update Department', 'text_domain' ),
    'view_item'                  => __( 'View Department', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate department with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove departments', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Departments', 'text_domain' ),
    'search_items'               => __( 'Search Departments', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No departments', 'text_domain' ),
    'items_list'                 => __( 'Departments list', 'text_domain' ),
    'items_list_navigation'      => __( 'Departments list navigation', 'text_domain' ),
  );
  $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
  );
  register_taxonomy( 'departments', 'user', $args );

}
add_action( 'init', 'custom_user_taxonomy', 0 );

Step 2: Add the Admin page for the custom Taxonomy by adding the code below to your functions.php file or to a plugin file. We will add this page as a sub-item of the Users menu on the WordPress dashboard. This page will allow us to define the different more specific departments that will be options in our custom taxonomy like ‘Sales’ and ‘IT’. Don’t forget to actually add those departments to the taxonomy or no choices will be rendered:

/**
* Admin page for the 'departments' taxonomy
*/
function cb_add_departments_taxonomy_admin_page() {

 $tax = get_taxonomy( 'departments' );

 add_users_page(
esc_attr( $tax->labels->menu_name ),
esc_attr( $tax->labels->menu_name ),
$tax->cap->manage_terms,
'edit-tags.php?taxonomy=' . $tax->name
 );

}
add_action( 'admin_menu', 'cb_add_departments_taxonomy_admin_page' );

/* Step 3 */

As the next step we are going to add the code below to… you guessed it, the functions.php file or a plugin file. The code below is going to add the taxonomy we defined in the previous steps to the profile of every user so that we can set each user’s department.

function cb_edit_user_department_section( $user ) {
  global $pagenow;

  $tax = get_taxonomy( 'departments' );

  /* Make sure the user can assign terms of the departments taxonomy before proceeding. */
  if ( !current_user_can( $tax->cap->assign_terms ) )
    return;

  /* Get the terms of the 'departments' taxonomy. */
  $terms = get_terms( 'departments', array( 'hide_empty' => false ) ); ?>

  <h3><?php _e( 'Departments' ); ?></h3>

  <table class="form-table">

    <tr>
      <th><label for="departments"><?php _e( 'Allocated Departments' ); ?></label></th>

      <td><?php

      /* If there are any departments terms, loop through them and display checkboxes. */
      if ( !empty( $terms ) ) {
          echo cb_custom_form_field('departments', $terms, $user->ID);
      }

      /* If there are no departments terms, display a message. */
      else {
        _e( 'There are no departments available.' );
      }

      ?></td>
    </tr>

  </table>
<?php }

add_action( 'show_user_profile', 'cb_edit_user_department_section' );
add_action( 'edit_user_profile', 'cb_edit_user_department_section' );
add_action( 'user_new_form', 'cb_edit_user_department_section' );

/* Step 4 */
Define the function for saving the custom taxonomy by putting this code into… well the functions.php file or a plugin file. You probably get that by now but the code below does define that function:

/**
 * @param int $user_id The ID of the user to save the terms for.
 */
function cb_save_user_department_terms( $user_id ) {

  $tax = get_taxonomy( 'departments' );

  /* Make sure the current user can edit the user and assign terms before proceeding. */
  if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
    return false;

  $term = $_POST['departments'];
  $terms = is_array($term) ? $term : (int) $term; // fix for checkbox and select input field

  /* Sets the terms (we're just using a single term) for the user. */
  wp_set_object_terms( $user_id, $terms, 'departments', false);

  clean_object_term_cache( $user_id, 'departments' );
}

add_action( 'personal_options_update', 'cb_save_user_department_terms' );
add_action( 'edit_user_profile_update', 'cb_save_user_department_terms' );
add_action( 'user_register', 'cb_save_user_department_terms' );

That was it! Four steps to add this awesome functionality. This data stored per user is very useful for having different types or tiers of users that have access to special content that is hidden from everyone else. Happy WordPressing!

Categories
Web Development Website WordPress

WP Employees

This is a small WordPress plugin I wrote to use for an issue I ran into a lot as a freelancer. Many small businesses want to make a “Team Page” to showcase their staff and make the business more personal. I found myself getting needing to create these same team pages for multiple clients on WordPress.

Ahh, that sounds like an opportunity for me to work some WordPress magic, and use Custom Post Types along with custom fields to generate quick team pages.

So I did just that using PHP, HTML and CSS. The code is available on my GitHub at the link below. Happy WordPressing!

Categories
Web Development

Development Toolbox

There are many tools for a developer to use to create beautiful WordPress sites, here are a few that I have used and recommend for any budding WordPress dev:

Local Emulator

Laravel Valet, Local by Flywheel, MAMP, WAMP, XAMP

Database Editing Tool

Sequel Pro, phpmyadmin, adminer, MYSQL Workbench

OS / Server OS

Mac OS, Ubuntu, Windows (not the easiest choice but doable)

Browser

chrome, Firefox, Edge, Opera

Plugins

iThemes Security, Yoast, WP Optimize, WP Monitor

Categories
Web Development Website WordPress

WordPress: Advanced Custom Fields or ACF

This powerful plugin is a great way to graphically add beautiful fields and data to every post of a certain type, extending the already-rich WordPress functionality in a great way that I personally love and use all the time.

ACF allows you to make logical rules about when certain fields are shown, which post types they are for, how they are accessed, conditional logic for given fields and a whole lot more. There are not enough good things for me to say about this plugin.

There are plenty of other ways to add fields to WordPress posts since it is such a useful functionality but this uses easy-to-create logic rules to add fields where they are needed instead of relying on code which many of the other solutions do which is limiting.

The ACF pro version also allows for the creation of custom blocks for the Gutenberg editor. I will not explain all of the nuances of block creation, but ACF is a great way to graphically achieve a lot of great things in a WordPress site without having to write much code! (although values given into ACF are accessible via code too!)

Categories
Web Development Website WordPress

WordPress: Better Search Replace

This next plugin is a great tool for anyone building or migrating a WordPress site. As we know, WordPress sites are all based on databases that are usually created just for them.

Better Search Replace (BSR) allows the user to search all or certain tables in their database for a word or words and either erase them or change them to something else. If a company with a WordPress website was just purchased by another company and the name was changed, no problem. This plugin can find all instances of the old name and replace each one with the new name.

Categories
Web Development Website

Awesome Client Genewiz

I have been working with this cool client for a few months, Genewiz, who does work with genetics and genetic testing. It sounds like my work was with that interesting laboratory science work but I am just doing some web work for them.

Everyone here is very nice and this is an interesting company but I am only here temporarily for freelance work.

Categories
Travel Web Development

Generate2016

I recently attended a conference in NYC called Generate2016. It was interesting and very different from a WordCamp. The attendance was big and the tickets were expensive, but the conference itself was interesting.

They talked about technology in general, not just WordPress websites like I was used to. There were interesting talks about robotics as well as websites and even some about the expanding the limitations of technology.

Categories
Web Development Website

NJ Devils

I designed the mascot page for the NJ Devils! The Devils use some other strange CMS that I do not like as much as WordPress but it was fun to work around the rink where the team plays and concerts for various famous artists are held.

Categories
Web Development

Use onTouch instead of onClick for quicker reaction to an event

In JQuery, there is included the “tap” event, which reacts with less of a delay to touches on smartphones. Behind the scenes, it’s bound to touchstart or mousedown. Using the aforementioned event is good for apps that are either solely meant for smartphones or ones that are meant to be used on either smartphones or desktop computers (such as websites).

The onClick event still works in all of the same situations, the developers of JQuery have just added this new event to watch in order to make coding more intuitively worded and to cut down on some of the delay times.