- Setup local version for site development using LocalWP
- Create github repositories to store repo’s for folders you will develop, typically a plugin you are developing
- Create a file changelog.md
- Add all changes to the change log
- After each development stage, commit changes to the Github Repo
- When ready, create a dev site on the hosting server and test on dev site
- Only when dev site is updated to match live site ( if it exists), update live site
WordPress Development
wp_get_current_user()
Purpose
- Retrieves the current user object.
Implementation
- Include in
- Plugin
- functions.php of active theme
<?php
$current_user = wp_get_current_user();
Example of output of Object $current_user
WP_User Object
(
[data] => stdClass Object
(
[ID] => 8
[user_login] => subscriber
[user_pass] => {Users Password}
[user_nicename] => subscriber
[user_email] => subscriber@sblik.com
[user_url] =>
[user_registered] => 2023-05-02 19:41:27
[user_activation_key] =>
[user_status] => 0
[display_name] => Subscriber Sblik
)
[ID] => 8
[caps] => Array
(
[subscriber] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => subscriber
)
[allcaps] => Array
(
[read] => 1
[level_0] => 1
[subscriber] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)
WordPress Get Logged In User Info for Front End
Objective
Get logged in user information to use in javascript.
- In order to use user information on the front end with Javascript or jQuery, you can use wp_localize_script
- wp_localize_script creates a Javascript object that can be accessed by javascript.
- function: wp_localize_script($handle, $nameOfObject, $dataStoredInObject)
- $handle: script handle the data will be attached to
- $nameOfObject: Name of Javascript Object
- $dataStoredInObject: single or multi-dimensional array
- Reference
Step 1: How to include wp_localize_script in a plugin
<?php
/**
* Plugin Name: Bliksem Sandbox Plugin
* Version: 1.0.0
* Description: Sandbox development for
* Author: Andre Nell
* Author URI: http://www.simplifysmallbiz.com/
*
*
* @package Bliksem
* @author Andre Nell
* @since 1.0.0
*/
if (!function_exists('get_option')) {
header('HTTP/1.0 403 Forbidden');
die; // Silence is golden, direct call is prohibited
}
// DEFINE CONSTANTS
define('BS_NAME_VERSION', '1.0.1');
define('BS_NAME_PLUGIN_URL', plugin_dir_url(__FILE__));
// IMPLEMENTATION
function bs_enqueue_bs_frontend_scripts()
{
global $current_user;
$current_user = wp_get_current_user();
wp_register_script('bs-sandbox-js', BS_NAME_PLUGIN_URL . 'js/sandbox.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('bs-sandbox-js');
wp_localize_script('bs-sandbox-js', 'theUser', array(
'roles' => $current_user->roles,
));
}
add_action('wp_enqueue_scripts', 'bs_enqueue_bs_frontend_scripts');
Step2: How to access the data stored in the Javascript Object created in Step 1
jQuery(window).load(function ($) {
var user_roles = theUser.roles;
console.log(user_roles);
if (jQuery.inArray("Subscriber", user_roles)) {
console.log("is in array");
} else {
console.log("is NOT in array");
}
});
Install MySQL Database in SiteGround
- Do Items
- Go to https://login.siteground.com
- Login
- Navigate to Websites
- Select ‘Site Tool’ for desired domain
- From menu on left, navigate to ‘SITE/MySQL
- Select ‘DATABASES’ in main content section
- Select ‘CREATE DATABASE’
- From the newly created database, click on Actions on far left column
- Select ‘Change Label’ and add a descriptive label to identify the database, for example, the subdomain address
- Select ‘CONFIRM’
- Select ‘USERS’ in main content section
- Select ‘CREATE USER’
- From the newly created user, click on Actions on far left column
- Select ‘Change Label’ and add a descriptive label to identify the user, for example, the subdomain address
- Select ‘CONFIRM’
- From the top of the main section, select ‘MANAGE ACCESS’
- Select the desired database
- Select ‘All Privileges’
- Select ‘CONFIRM’
Install fresh site from Duplicator Pro Archives
- Do Items
- Go to https://login.siteground.com
- Login
- Navigate to Websites
- Select ‘Site Tool’ for desired domain
- From menu on left, navigate to ‘SITE/File Manager
- Select public_html folder the desired domain or subdomain
- If folder is not empty, delete contents
- Upload Installer file from Duplicator Pro Archive
- Upload Archive file form Duplicator Pro Archive
- Copy the name of the installer file including the suffix
- Open a new browser tab
- Enter the domain or subdomain followed by /~name_of_installer_file.php
- Enter the name of the newly created or existing database
- Enter the name of the newly created or existing user
- Enter the password for the newly created or existing user. If necessary, navigate to the user, select ‘actions’, ‘Change Password’, ‘Generate’, ‘Copy’, and ‘CONFIRM’
- Select ‘VALIDATE’
- Check ‘I have read and accept all terms & notices*’
- Select ‘Next’
- On popup ‘Install Confirmation’ select ‘OK’.
- Files will extract and install
- Select ‘Admin Login’
- Login using admin credentials
How to install a site from Duplicator Pro to Local
- Do Items
- Open Local app
- Add new site
- Name new site (best to name local site same as site you are uploading from Duplicator Pro)
- Choose Your Environment – choose ‘Preferred’
- Add username, password, and email for site
- Change web server to ‘Apache’Navigate to Local sites folder in your computer files (Finder)
- Go to your Local site folder /app/public and delete all files in the public folder
- Navigate to the folder that holds your Duplicator Pro backup files
- Copy the latest installer and archive files of the site you want to put in Local
- Navigate back to your local site public folder
- Paste the installer and archive files into the public folder
- Navigate to your site in the Local app
- Click ‘Open Site’
- Click on the installer.php file in the webpage that opens
- Database is localhost
- User is root
- Password is root
- Click ‘Validate’
- Click ‘I have read and accepted all terms and notices’
- Click ‘Next’