Skip to content

Instantly share code, notes, and snippets.

View krystyna93's full-sized avatar
🏠
Working from home

krystyna krystyna93

🏠
Working from home
  • Victoria, Australia
View GitHub Profile
@krystyna93
krystyna93 / functions.php
Created June 16, 2023 14:08
WordPress 'Load More' Functionality with Ajax
<?php
function load_more_scripts() {
wp_enqueue_script( 'load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array('jquery'), '1.0', true );
wp_localize_script( 'load-more', 'load_more_params', array(
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
'security' => wp_create_nonce( 'load_more_security' ),
));
}
add_action( 'wp_enqueue_scripts', 'load_more_scripts' );
@krystyna93
krystyna93 / functions.php
Created June 16, 2023 14:05
WordPress 'Load More' Functionality w/o Ajax
<?php
function load_more_scripts() {
wp_enqueue_script( 'load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'load_more_scripts' );
function load_more_posts() {
check_ajax_referer( 'load_more_security', 'security' );
$paged = $_POST['page'];
$post_type = isset($_POST['post_type']) ? $_POST['post_type'] : 'post';
@krystyna93
krystyna93 / header.php
Last active June 13, 2023 08:03
Custom WordPress Displaying Different Banner Images on Pages
<?php
<?php
// Get current page ID and sanitize the input data
$page_id = absint(get_queried_object_id());
// Define image URLs for each page ID (using HTTPS)
$banner_images = array(
1 => 'https://example.com/banner-1.jpg',
2 => 'https://example.com/banner-2.jpg',
@krystyna93
krystyna93 / testimonials.php
Created June 13, 2023 07:35
Custom WordPress Testimonials Metabox
<?php
// Add a new metabox to the testimonial post type
function add_testimonial_metabox() {
// Use the WordPress function add_meta_box to define our metabox
add_meta_box(
'testimonial_metabox', // Metabox ID
'Testimonial Details', // Metabox title
'render_testimonial_metabox', // Callback function to render the metabox contents
'testimonial', // Post type where the metabox should appear
@krystyna93
krystyna93 / template-contact-form.php
Created June 13, 2023 07:27
Custom WordPress Contact Form
<?php
function display_encrypted_contact_form() {
// Initialize variables
$errors = array();
$success = false;
// Check if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate nonce
@krystyna93
krystyna93 / functions.php
Created June 13, 2023 05:09
WordPress Theme Checking Environment Type Local/Online/Development Mode
<?php
function mytheme_enqueue_scripts() {
wp_enqueue_style( 'mytheme-style', get_stylesheet_uri() );
// Check if we're in a local development environment or accessing from localhost
if ( 'local' === wp_get_environment_type() || in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')) ) {
// Enqueue the local development version of the script over HTTP
wp_enqueue_script( 'mytheme-scripts', 'http://localhost:8080/bundle.js', array(), '1.0.0', true );
} elseif ( WP_DEBUG ) {
@krystyna93
krystyna93 / functions.php
Last active June 10, 2023 14:51
Custom WordPress Metabox: Relationship Between Two CPT's
<?php
/*
a general overview of how to create a WordPress metabox that establishes a relationship between two custom post types and shares
details between them both, then output the data to the front end.
You will need to first register your custom post types using the register_post_type() function in your WordPress theme's functions.php
file.
Create a metabox for one of the custom post types using the add_meta_box() function. Within the metabox callback function
you can use the metadata API to retrieve data related to the other custom post type.
@krystyna93
krystyna93 / functions.php
Created June 10, 2023 14:16
Custom WordPress Metabox: Relation between two CPT's
<?php
/*
To create a relationship metabox between two custom post types in WordPress, you'll need to use the add_meta_box() function
and the WP_Query class. Here are the steps you can follow:
Create a custom post type using the register_post_type() function for both post types.
In the functions.php file of your theme or plugin, add the following code to create a metabox:
*/
@krystyna93
krystyna93 / functions.php
Created June 10, 2023 14:03
Custom WordPress Query: CPT Related Tax Terms
<?php
// Validate and sanitize the ID of the current post
$portfolio_id = ( isset( $_GET['post'] ) && ctype_digit( $_GET['post'] ) ) ? $_GET['post'] : '';
$portfolio_id = filter_var( $portfolio_id, FILTER_SANITIZE_NUMBER_INT );
// Verify the nonce before processing the form submission
if ( isset( $_POST['submit_related_portfolio'] ) && wp_verify_nonce( $_POST['related_portfolio_nonce'], 'related_portfolio' ) ) {
// Sanitize the input data
@krystyna93
krystyna93 / functions.php
Last active June 10, 2023 13:45
Custom Featured Image Metabox: Text Field for Image From URL
<?php
/*
The purpose of this field is to allow you to enter a custom URL for an image, which can be used as the featured image for your
WordPress post or page. This could be useful if you have an image that you want to use as the featured image, but it is not hosted
on your own website and therefore cannot be easily uploaded through the WordPress media library.
By adding this custom field, you can enter the URL of the external image and then use it as the featured image for your post or page,
just as you would with a standard image uploaded through the WordPress media library.
*/