• Skip to main content
  • Skip to primary sidebar

WordPress, Genesis Framework and Storefront customization tutorials

  • Archive
    • Genesis
    • WooCommerce
    • WordPress
  • Premium
  • Blog
  • About
    • Tools I Use
  • Contact
  • Login
Home » PHP

PHP

Show posts with descending order number

2021-01-07 by Gabor Leave a Comment

Filed Under: PHP, WordPress

How to Show Total Number of Posts, Pages, or Custom Post Types

2021-01-07 by Gabor Leave a Comment

Filed Under: PHP, WordPress

AJAX Filter for WordPress Custom Post Type Taxonomy

2020-12-06 by Gabor Leave a Comment

View Demo

Step #1 - Enqueue scripts in functions.php



Step #2 - Build the filter



To view the full content, please sign up for the membership.

If You are already a member, please log in below:

 
 
Forgot Password

Filed Under: JavaScript, PHP, Premium Content, UI / UX Tagged With: ajax

AJAX Filter for WordPress Categories without Plugin

2020-12-04 by Gabor Leave a Comment

View Demo

Step #1 - Enqueue scripts in functions.php



To view the full content, please sign up for the membership.

If You are already a member, please log in below:

Filed Under: JavaScript, PHP, Premium Content, UI / UX, WordPress Tagged With: ajax

AJAX request from MySQL database

2020-12-03 by Gabor Leave a Comment

HTML

PHP

JavaScript

View GitHub

Filed Under: JavaScript, PHP, SQL Tagged With: ajax

AJAX – Submit data with POST request into a MySQL database

2020-12-03 by Gabor Leave a Comment

To make it work you will need to create a database in phpMyAdmin.

HTML

PHP

JS

View GitHub

Filed Under: JavaScript, PHP Tagged With: ajax

Display Google Maps location address details of ACF field without coordinates

2020-08-15 by Gabor Leave a Comment

<?php 
$location = get_field('location');
if( $location ) {

    // Loop over segments and construct HTML.
    $address = '';
    foreach( array('street_number', 'street_name', 'city', 'state', 'post_code', 'country') as $i => $k ) {
        if( isset( $location[ $k ] ) ) {
            $address .= sprintf( '<span class="segment-%s">%s</span>, ', $k, $location[ $k ] );
        }
    }

    // Trim trailing comma.
    $address = trim( $address, ', ' );

    // Display HTML.
    echo '<p>' . $address . '.</p>';
}

Filed Under: PHP Tagged With: ACF, Google Maps

Add page slug to body_class

2019-11-19 by Gabor Leave a Comment

// =========================================================================
// ADD PAGE SLUG TO BODY CLASS
// =========================================================================
function add_slug_to_body_class( $classes ) {
    global $post;
    if ( isset( $post ) ) {
        $classes[] = $post->post_name;
    }
    return $classes;
}
add_filter( 'body_class', 'add_slug_to_body_class' );

Filed Under: Free Content, PHP, WordPress Tagged With: body_class

Get videos from channel by YouTube Data API

2019-06-27 by Gabor Leave a Comment

To view the full content, please sign up for the membership.

If You are already a member, please log in below:

Filed Under: PHP, Premium Content Tagged With: API, YouTube

Remove Entry Meta from Genesis Single Post

2019-01-07 by Gabor Leave a Comment

//Remove Entry Meta from Genesis Single Post
add_action( 'loop_start', 'remove_entry_meta' );
function remove_entry_meta() {
if ( is_singular('post') ) {
    remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
    remove_action( 'genesis_entry_footer', 'genesis_post_meta' );

    }
}

Filed Under: Genesis, PHP

Custom Post Type Taxonomy Filter in Admin

2018-11-13 by Gabor

<?php

add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy');
function tsm_filter_post_type_by_taxonomy() {
	global $typenow;
	$post_type = 'YOUR-POST-TYPE'; // change to your post type
	$taxonomy  = 'YOUR-TAXONOMY'; // change to your taxonomy
	if ($typenow == $post_type) {
		$selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
		$info_taxonomy = get_taxonomy($taxonomy);
		wp_dropdown_categories(array(
			'show_option_all' => __("Összes kategória"),
			'taxonomy'        => $taxonomy,
			'name'            => $taxonomy,
			'orderby'         => 'name',
			'selected'        => $selected,
			'show_count'      => true,
			'hide_empty'      => true,
		));
	};
}

add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
	global $pagenow;
	$post_type = 'YOUR-POST-TYPE'; // change to your post type
	$taxonomy  = 'YOUR-TAXONOMY'; // change to your taxonomy
	$q_vars    = &$query->query_vars;
	if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
		$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
		$q_vars[$taxonomy] = $term->slug;
	}
}

Filed Under: PHP, WordPress

How to set Featured Image as Background Cover with Title and Sub Title

2018-10-11 by Gabor

Create a hook in functions.php

//ADD COVER TO ONLY SUB PAGES
function add_background_cover(){
    if ( !is_front_page() ) {
        include('inc/cover.php');
    }
}
add_action('genesis_after_header', 'add_background_cover');

Create a file inc/cover.php and place the following snippet in it. Don’t forget to replace the ACF slug in cover_subtitle() function!

<?php

//Cover Background
function cover_background() { 
    
    function cover_subtitle() { 
    if( get_field('........') ){ the_field('........') }
            else{ echo 'Hello World!'; }
        }
    
    $bgImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); 
    if( has_post_thumbnail() ){ ?>
            <section class="cover" style="background-image: url('<?php echo $bgImage[0]; ?>'); ">
                <h1 class="entry-title" itemprop="headline"><?php the_title(); ?></h1>
                <p><?php cover_subtitle(); ?></p>
            </section>
        <?php }
        else{  ?>
            <section class="cover" style="background-image: url(..............); ">
               <h1 class="entry-title" itemprop="headline"><?php the_title(); ?></h1>
                <p><?php cover_subtitle(); ?></p>
            </section>
            <?php
             }
    }
cover_background();
?> 

CSS

 .cover {
	height: 280px;
	padding-top: 60px;
}

Filed Under: Genesis, PHP, WordPress

Hide something if user logged out / Show only for logged in users

2018-10-10 by Gabor

Put these snippet in functions.php

//HIDE IF LOGGED OUT
function hide_if_logged_out(){
    if ( is_user_logged_in() ) {
           echo '';
        } else {
           echo 'hidden';
        }
    }

Call the function as class

<div class="container <?php echo hide_if_logged_out(); ?>">

Filed Under: PHP, WordPress

If there’s no post in the query

2018-07-15 by Gabor

<?php
    endwhile;
else:
    echo 'There is no posts yet.';
endif;
?>

Filed Under: PHP, WordPress

Loop Zillow CPT

2018-07-15 by Gabor

<?php
$the_query = new WP_Query( $args );
?>
 
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="review-content">
    

    
     <?php include('stars-main.php'); ?>
         <label class="highly">
             <?php if( get_field('main_review') == '5') : ?>
                 Highly likely to recommend
             <?php endif; ?>
             <?php if( get_field('main_review') == '4') : ?>
                 Likely to recommend
             <?php endif; ?>
             <?php if( get_field('main_review') == '3') : ?>
                 Might recommend
             <?php endif; ?>
         </label> <br>
     <ul class="review-metadata">
         <li><?php the_title(); ?></li>
         <li><?php the_field('short_description'); ?></li>
         <li>
                <?php if( get_field('primary_point_of_contact') ): ?>
                    <li><?php the_field('primary_point_of_contact'); ?></li>
                <?php endif; ?>
         </li>
     </ul>
     <div class="reviews-sub-ratings">
         <ul class="ratings">
             <li><span class="ratings-col">Local knowledge:</span> <?php include('stars-local.php'); ?></li>
             <li><span class="ratings-col">Process expertise:</span> <?php include('stars-process.php'); ?></li>
             <li><span class="ratings-col">Responsiveness:</span> <?php include('stars-respo.php'); ?></li>
             <li><span class="ratings-col">Negotiation skills:</span> <?php include('stars-nego.php'); ?></li>
         </ul>
     </div>
     <div class="review-body">
         <?php the_field('review'); ?>
     </div>
    
</div>

<?php
    endwhile;
else:
    echo 'No reviews yet.';
endif;
?>
 
<?php wp_reset_postdata(); ?>

Filed Under: PHP, WordPress

ACF hide if empty custom field

2018-07-14 by Gabor


 <!--ACF HIDE IF EPMTY CUSTOM FIELD -->
 <?php if( get_field('mobil') ): ?>
		<li><?php the_field('mobil'); ?></li>
	<?php endif; ?>

 


<!--IF ELSE HIDE - SHOW DIV IF THE_FILED IS EMPTY!!!!!!!-->              
<?php if( get_field('hogyan_keszult_link') ){ ?>
        <style type="text/css">.hogy-keszul-cta{
            display:block;
        }</style>
<?php
}?>

//CUSTOM FIELDS SELECET OPTIONS
<?php if( get_field('color') == 'red' ): ?>
<p>Selected the Red choice!</p>
<?php endif; ?>

Filed Under: Advanced Custom Fields, PHP, WordPress

Translate without PO Edit

2018-06-06 by Gabor


<?php
function wpf_filter_gettext( $translated, $original, $domain ) {
    if ( $translated == "OLD_TEXT" ) { $translated = "NEW_TEXT"; }
return $translated;
}
add_filter( 'gettext', 'wpf_filter_gettext', 10, 3 );

Remove translation


function remove_translation( $translated_text, $untranslated_text, $domain ) {

$removable_text = 'Ide kerül az eltávolítani kívánt kifejezés.';

if ($untranslated_text === $removable_text ) {
return '';
}

return $translated_text;
}

 

Filed Under: PHP, WordPress Tagged With: translation

PHP date format

2018-05-15 by Gabor

Required. Specifies the format of the outputted date string. The following characters can be used:

  • d – The day of the month (from 01 to 31)
  • D – A textual representation of a day (three letters)
  • j – The day of the month without leading zeros (1 to 31)
  • l (lowercase ‘L’) – A full textual representation of a day
  • N – The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
  • S – The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
  • w – A numeric representation of the day (0 for Sunday, 6 for Saturday)
  • z – The day of the year (from 0 through 365)
  • W – The ISO-8601 week number of year (weeks starting on Monday)
  • F – A full textual representation of a month (January through December)
  • m – A numeric representation of a month (from 01 to 12)
  • M – A short textual representation of a month (three letters)
  • n – A numeric representation of a month, without leading zeros (1 to 12)
  • t – The number of days in the given month
  • L – Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
  • o – The ISO-8601 year number
  • Y – A four digit representation of a year
  • y – A two digit representation of a year
  • a – Lowercase am or pm
  • A – Uppercase AM or PM
  • B – Swatch Internet time (000 to 999)
  • g – 12-hour format of an hour (1 to 12)
  • G – 24-hour format of an hour (0 to 23)
  • h – 12-hour format of an hour (01 to 12)
  • H – 24-hour format of an hour (00 to 23)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds, with leading zeros (00 to 59)
  • u – Microseconds (added in PHP 5.2.2)
  • e – The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
  • I (capital i) – Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
  • O – Difference to Greenwich time (GMT) in hours (Example: +0100)
  • P – Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T – Timezone abbreviations (Examples: EST, MDT)
  • Z – Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
  • c – The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
  • r – The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
  • U – The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Filed Under: PHP Tagged With: date

Add text via function

2018-05-13 by Gabor


//ADD TEXT VIA FUNCTION
add_filter( 'ultimatum_print_footer', 'add_barion_banner' );
function add_barion_banner( $barion_banner ) {
echo 'TESZT';
return $barion_banner;
}

Filed Under: PHP

Primary Sidebar

  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
WP Rocket - WordPress Caching Plugin
UpdraftPlus Premium

Disclosure: Some of the links in this site are affiliate links. I will be paid a commission if you use this link to make a purchase.

  • Privacy Policy
  • Flames Design
© 2021 WP Flames - All Right Reserved