Skip to content

Instantly share code, notes, and snippets.

View AnadarProSvcs's full-sized avatar

Anadar Professional Services LLC AnadarProSvcs

  • Anadar Professional Servces LLC
View GitHub Profile
@AnadarProSvcs
AnadarProSvcs / plug-in-cron.php
Last active December 2, 2023 18:53
Adding a cron job to a WordPress plug-in
// When the plugin is activated, schedule a weekly event for a specific task
function custom_plugin_activate() {
// Check if the event isn't already scheduled
if (!wp_next_scheduled('custom_task_hook')) {
// Schedule a weekly event using WordPress cron
wp_schedule_event(time(), 'weekly', 'custom_task_hook');
}
// Call the function to perform the task immediately upon activation
perform_custom_task();
}
@AnadarProSvcs
AnadarProSvcs / send-email-new-post.php
Created November 26, 2023 23:23
Sending an Email When New Posts Are Added in WordPress
// Hook to the post insertion action, to trigger a notification on certain post types
add_action('wp_insert_post', 'notify_admin_for_new_post', 10, 3);
function notify_admin_for_new_post($post_id, $post, $update)
{
// Check for a specific custom post type and if the post is being published for the first time
if ($post->post_type == 'custom_post_type' && $post->post_status == 'publish' && empty(get_post_meta($post_id, 'check_if_run_once'))) {
// Initialize variables
$organizer = '';
$post_title = get_the_title(); // Assuming it's the title of the post
@AnadarProSvcs
AnadarProSvcs / formidable-forms-checkbox-values.php
Last active November 27, 2023 13:25
Save the values of checkboxes from a Formidable Form
//You can use this to load the values from checkboxes in a Formidable form.
//In Formidable Forms for WordPress, when you want to retrieve the values of checkboxes using the frm_after_create_entry hook,
//you need to access the form entry data that's passed to your hook function.
//This hook is triggered after an entry is created, and it provides you with all the submitted data.
add_action('frm_after_create_entry', 'get_checkbox_values', 30, 2);
function get_checkbox_values($entry_id, $form_id){
// Check if it's the specific form you want (replace 123 with your form ID)
if ($form_id == 123) {
// Get the entry object
$entry = FrmEntry::getOne($entry_id, true);
@AnadarProSvcs
AnadarProSvcs / dragdrop.css
Last active December 13, 2023 14:22
This is a rough template for creating drag and drop/sortable lists in WordPress. This is very rough, but gives the frame work. The divs can be about as complex as you want them and created in any way appropriate. In a WordPress environment, you'd need to enqueue the javaScript and create the Ajax functions.
#sortable-area {
width: 100%;
min-height: 50px;
border: 1px solid #ccc;
padding: 10px;
}
.sortable-item {
margin: 10px;
padding: 5px;
@AnadarProSvcs
AnadarProSvcs / logoutredirect.php
Last active December 19, 2023 18:58
Redirect where a user goes when they log out of WordPress depending on their role. #wordpress
function custom_logout_redirect( $redirect_to, $requested_redirect_to, $user ) {
// Array of roles and their respective redirect URLs
$role_redirects = array(
'administrator' => home_url( '/admin-page/' ),
'editor' => home_url( '/editor-page/' ),
// Add other roles and their redirects here
);
// Check if user data is available
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
@AnadarProSvcs
AnadarProSvcs / wp_resize_image.php
Created December 17, 2023 19:22
Function to resize an uploaded image to a maximum width/height depending on orientation
function resize_uploaded_image($file_path, $max_width, $max_height) {
$image = wp_get_image_editor($file_path);
if (!is_wp_error($image)) {
$size = $image->get_size();
$current_width = $size['width'];
$current_height = $size['height'];
// Determine the orientation of the image
$orientation = $current_width > $current_height ? 'landscape' : 'portrait';
@AnadarProSvcs
AnadarProSvcs / wp_post.md
Last active January 20, 2024 14:28
WP_POST Object in WordPress - common functions and properties.

WordPress WP_Post Object Reference

Properties of WP_Post

  • ID: Unique identifier for the post.
  • post_author: ID of the post's author.
  • post_date: Date and time of post publication.
  • post_date_gmt: Date and time of post publication in GMT.
  • post_content: Full content of the post.
  • post_title: Title of the post.
@AnadarProSvcs
AnadarProSvcs / wp_user.md
Last active December 19, 2023 18:57
WP_User the WordPress User object common properties and functions #wordpress

WordPress WP_User Object Reference

Properties of WP_User

  • ID: Unique identifier for the user.
  • user_login: The user's login name.
  • user_pass: The user's password (hashed).
  • user_nicename: The URL-friendly user name.
  • user_email: The user's email address.
  • user_url: The user's website URL.
@AnadarProSvcs
AnadarProSvcs / wp_query.md
Last active December 19, 2023 18:56
WP_Query - An overview of the WP_Query in WordPress #wordpress

WordPress WP_Query Class Reference

Overview

WP_Query is a powerful class in WordPress used to query posts. It allows developers to specify a variety of parameters to retrieve posts, pages, custom post types, and more.

Key Properties

  • query: The query variables set in the new WP_Query instance.
  • query_vars: An array of query variables.
  • tax_query: The taxonomy query object.
  • meta_query: The meta query object.
@AnadarProSvcs
AnadarProSvcs / wp_term.md
Last active December 19, 2023 18:57
An over view of the WP_Term object from WordPress. WP_Term objects are often returned by functions like get_terms and get_term, and provide a convenient way to access the properties of individual terms. #wordpress

WordPress WP_Term Class Reference

Overview

WP_Term is a class in WordPress that represents terms within taxonomies such as categories, tags, or custom taxonomies.

Key Properties

  • term_id: The ID of the term.
  • name: The name of the term.
  • slug: The slug of the term, used in URLs.
  • term_group: The group that the term belongs to.