• Skip to main content
  • Skip to primary sidebar

WordPress, Genesis Framework and Storefront customization tutorials

  • Archive
    • Free
    • Premium
  • Blog
  • About
  • Contact
  • Newsletter
  • Login
Home » PHP » Page 3

PHP

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

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: Free Tagged With: PHP

How to get post title slug

<?php 
    global $post;
    $post_slug=$post->post_name;
?>

Filed Under: Free Tagged With: PHP

How to set Excerpt Length

Add this snippet to functions.php

Filed Under: Free Tagged With: PHP

Unable to translate “Shipping” string in Checkout review table

add_filter( 'woocommerce_shipping_package_name', 'custom_shipping_package_name' );
function custom_shipping_package_name( $name ) {
  return 'Szállítás';
}

Filed Under: Free Tagged With: PHP

How to rename Posts post type in WordPress admin

/* Alapértelmezett WordPress 'Bejegyzés' átnevezése */
function revcon_change_post_label() {
    global $menu;
    global $submenu;
    $menu[5][0] = 'Blog';
	$menu[5][6] = 'dashicons-format-status';
    $submenu['edit.php'][5][0] = 'Összes blogbejegyzés';
    $submenu['edit.php'][10][0] = 'Új bejegyzés írása';
    //$submenu['edit.php'][16][0] = 'News Tags';
    echo '';
}
function revcon_change_post_object() {
    global $wp_post_types;
    $labels = &$wp_post_types['post']->labels;
    $labels->name = 'Blog';
    $labels->singular_name = 'News';
    $labels->add_new = 'Új bejegyzés írása';
    $labels->add_new_item = 'Új bejegyzés írása';
    $labels->edit_item = 'Edit News';
    $labels->new_item = 'News';
    $labels->view_item = 'View News';
    $labels->search_items = 'Blogbejegyzés keresése';
    //$labels->not_found = 'No News found';
    //$labels->not_found_in_trash = 'No News found in Trash';
    $labels->all_items = 'Összes blogbejegyzés';
    $labels->menu_name = 'Blog';
    $labels->name_admin_bar = 'Blog';
}
 
add_action( 'admin_menu', 'revcon_change_post_label' );
add_action( 'init', 'revcon_change_post_object' );

Filed Under: Free Tagged With: PHP

How to add custom field to WordPress Customizer

<?php

add_action( 'customize_register', 'wts_register_theme_customizer' );
/*
 * Register Our Customizer Stuff Here
 */
function wts_register_theme_customizer( $wp_customize ) {
	// Create custom panel.
	$wp_customize->add_panel( 'text_blocks', array(
		'priority'       => 500,
		'theme_supports' => '',
		'title'          => __( 'Text Blocks', 'wts' ),
		'description'    => __( 'Set editable text for certain content.', 'wts' ),
	) );
	// Add section.
	$wp_customize->add_section( 'custom_cta_title_text' , array(
		'title'    => __('Change CTA Title Text','wts'),
		'panel'    => 'text_blocks',
		'priority' => 10
	) );
	// Add setting
	$wp_customize->add_setting( 'cta_title_text_block', array(
		 'default'           => __( 'default text', 'wts' ),
		 'sanitize_callback' => 'sanitize_text'
	) );
	// Add control
	$wp_customize->add_control( new WP_Customize_Control(
	    $wp_customize,
		'custom_cta_title_text',
		    array(
		        'label'    => __( 'CTA Title Text', 'wts' ),
		        'section'  => 'custom_cta_title_text',
		        'settings' => 'cta_title_text_block',
		        'type'     => 'text'
		    )
	    )
	);

 	// Sanitize text
	function sanitize_text( $text ) {
	    return sanitize_text_field( $text );
	}
}

Displaying in the Front End

<?php echo get_theme_mod( 'cta_title_text_block'); ?>

Filed Under: Free Tagged With: PHP

WordPress Loop by Specific Page ID

<?php $kapcsolat_query = new WP_Query(array( 'post_type' => array('page'), 'post__in' => array(760) )); ?>

<?php while ($kapcsolat_query->have_posts()) : $kapcsolat_query->the_post(); ?>

<?php endwhile; ?>

Filed Under: Free Tagged With: PHP

How to set featured image as background cover

Echo the return value from wp_get_attachment_image_src(). It also returns an Array(), so you need to grab the needed part from that array. In this case it’s the first/0 value. Example:

<?php $bgImage = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>

Include tha variable inside the inline stylesheet

<section class="contact-cover" style="
    background: url('<?php echo $bgImage[0]; ?>');
    background-size: cover;
    background-attachment: fixed;
    padding: 60px 0;">
</section>

Filed Under: Free Tagged With: CSS, PHP

How to get post title slug


<?php 
    global $post;
    $post_slug=$post->post_name;
?>

Filed Under: Free Tagged With: PHP

Loop Popular Posts

Add this snippet to functions.php

function shapeSpace_popular_posts($post_id) {
	$count_key = 'popular_posts';
	$count = get_post_meta($post_id, $count_key, true);
	if ($count == '') {
		$count = 0;
		delete_post_meta($post_id, $count_key);
		add_post_meta($post_id, $count_key, '0');
	} else {
		$count++;
		update_post_meta($post_id, $count_key, $count);
	}
}
function shapeSpace_track_posts($post_id) {
	if (!is_single()) return;
	if (empty($post_id)) {
		global $post;
		$post_id = $post->ID;
	}
	shapeSpace_popular_posts($post_id);
}
add_action('wp_head', 'shapeSpace_track_posts');

Displaying Popular Posts


<h3>Legnépszerűbb bejegyzések</h3>
<?php 
$args = array(
        'posts_per_page'=>3, 
        'meta_key'=>'popular_posts', 
        'orderby'=>'meta_value_num', 
        'order'=>'DESC'
);

$popular = new WP_Query( $args ); ?>

<div class="popular-posts">
<?php 	while ($popular->have_posts()) : $popular->the_post(); ?>
	<div class="row">
        <div class="col-md-4">
            <?php the_post_thumbnail( 'full', array('class' => 'img-responsive ') ); ?>
        </div>
        <div class="col-md-8">
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
            <time class="entry-date"><?php the_date('Y-m-d'); ?></time>
        </div>
    </div>
<?php endwhile; wp_reset_postdata(); ?>
</div>

Filed Under: Premium Tagged With: PHP

Example of the comments_template() filter

	/* 
	 *
	 * 	Let's us override the default comment template
	 *	located at wp-includes/comment-template.php	and
	 * 	use our own comment file instead.	 
	 *
	*/

	function custom_comment_template( $comment_template ) {

        return dirname(__FILE__) . '/inc/custom-comment-template.php';

	}

	add_filter( 'comments_template', 'custom_comment_template' );

Filed Under: Free Tagged With: PHP

Example of the customize_register() action hook

/* 
	 *
	 * 	Let's us add options for customizing our theme
	 * 	from the build in WordPress Customize screen.
	 * 	Gives access to four methods we can use with 
	 *	the hook.	 
	 *	 
	*/
function my_theme_customizations( $wp_customize )
	{
	   	/*
	     *	The $wp_customize object we pass as a parameter 
	     *	gives us access to the following methods:
	     *
	     * 	- add_setting()
	     *	- add_section()
	     * 	- add_control()
		 * 	- get_setting()
		 *
		*/

	}
	add_action( 'customize_register', 'my_theme_customizations' );


Filed Under: Free Tagged With: PHP

Example of the admin_head() action hook

/* 
	 * 	Let's us display code inside of the <head>
	 * 	tags in the WordPress admin area.
	 * 	We will use this to custom CSS for our plugin
	 * 	with the WordPress admin area
	 *
	*/
function my_plugin_back_end_css() {

		wp_enqueue_style( 'my_plugin_back_end_css', plugins_url('css/back-end.css', __FILE__) );    

	}
	add_action( 'admin_head', 'my_plugin_back_end_css' );

Filed Under: Free Tagged With: PHP

Enqueue Google Font

If You want to use Google Font on your WordPress site, You will need to enqueue it in the functions.php

Now just add the following CSS

* {
    font-family: 'Poppins', sans-serif;
}

Filed Under: Free Tagged With: PHP

Loop team members

<?php
wp_reset_postdata();

$args = array(
'post_type' => 'team',
'posts_per_page' => 100
);

$the_query = new WP_Query( $args );
?>
<div class="container">
<div class="row">
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="col-md-4">

<div class="card member">
<a href="<?php the_permalink(); ?>">
<img class="card-img-top" src="<?php the_field('image'); ?>" alt="">
</a>
<div class="card-body">
<h3 class="card-title"><?php the_title(); ?></h3>
<p class="card-text hidden"><?php the_field('description'); ?></p>
<a class="hidden" href="<?php the_permalink(); ?>" class="btn btn-primary">Read more</a>
</div>
</div>
</div>

<?php endwhile; endif; ?>

</div><!--row-->

</div><!--container-->

<?php wp_reset_postdata(); ?>

Filed Under: Free Tagged With: PHP

Simple shortcode function

If the shortcode produces a lot of HTML then ob_start can be used to capture output and convert it to a string as follows:

Filed Under: Free Tagged With: PHP

Add custom widget to anywhere you want

Register widget in functions.php

 
// =========================================================================
// REGISTER NEW WIDGET
// =========================================================================
function custom_widgets_init() {

    register_sidebar( array(
            'name' => 'Custom Widget',
            'id' => 'custom-widget-id',
            'before_widget' => '<div class="custom-widget">',
            'after_widget' => '</div>',
            'before_title' => '<h4 class="custom-title">',
            'after_title' => '</h4>',
        ) );

}
add_action( 'widgets_init', 'custom_widgets_init' ); 

Place this code where you want to appear the widget


<?php dynamic_sidebar('Custom Widget'); ?> 

Filed Under: Free Tagged With: PHP

Translate without PO Edit


<?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: Free Tagged With: PHP

Disable Comments in WordPress

<?php
/**
 * Plugin Name: Disable Comments
 * Plugin URI: https://codex.wordpress.org
 * Author: @wpflames
 */

// Disable support for comments and trackbacks in post types
function df_disable_comments_post_types_support() {
	$post_types = get_post_types();
	foreach ($post_types as $post_type) {
		if(post_type_supports($post_type, 'comments')) {
			remove_post_type_support($post_type, 'comments');
			remove_post_type_support($post_type, 'trackbacks');
		}
	}
}
add_action('admin_init', 'df_disable_comments_post_types_support');

// Close comments on the front-end
function df_disable_comments_status() {
	return false;
}
add_filter('comments_open', 'df_disable_comments_status', 20, 2);
add_filter('pings_open', 'df_disable_comments_status', 20, 2);

// Hide existing comments
function df_disable_comments_hide_existing_comments($comments) {
	$comments = array();
	return $comments;
}
add_filter('comments_array', 'df_disable_comments_hide_existing_comments', 10, 2);

// Remove comments page in menu
function df_disable_comments_admin_menu() {
	remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'df_disable_comments_admin_menu');

// Redirect any user trying to access comments page
function df_disable_comments_admin_menu_redirect() {
	global $pagenow;
	if ($pagenow === 'edit-comments.php') {
		wp_redirect(admin_url()); exit;
	}
}
add_action('admin_init', 'df_disable_comments_admin_menu_redirect');

// Remove comments metabox from dashboard
function df_disable_comments_dashboard() {
	remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'df_disable_comments_dashboard');

// Remove comments links from admin bar
function df_disable_comments_admin_bar() {
	if (is_admin_bar_showing()) {
		remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
	}
}
add_action('init', 'df_disable_comments_admin_bar');
//END DISABLE COMMENTS




//REMOVE LOST PASSWORD
function remove_lostpassword_text ( $text ) {
	 if ($text == 'Elfelejtett jelszó?'){$text = '';}
		return $text;
	 }
add_filter( 'gettext', 'remove_lostpassword_text' );

Filed Under: Free Tagged With: PHP

Customize search.php

function genesis_do_search_title() {

global $wp_query;
$total_results = $wp_query->found_posts;

$title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s '. $total_results .' találat a "%s" kifejezésre </h1></div>', apply_filters( 'genesis_search_title_text', __( 'Search Results for:', 'genesis' ) ), get_search_query() );

echo apply_filters( 'genesis_search_title_output', $title ) . "\n";

}

Filed Under: Free Tagged With: PHP

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Go to Next Page »

Primary Sidebar

Gabor Flamich

Hi! I'm Gabor.
I write tutorials on WordPress and WooCommerce.

MacBook

12 Essential Snippets for Genesis Developers

Subscribe to my Newsletter to view my basic collection of Genesis snippets that I use for my projects!

Sign Up for Free
  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube
UpdraftPlus Premium

Tags

ACF Ajax Analytics API Bootstrap Breadcrumb category CPT CSS fetch FSE Genesis Google Maps Gutenberg HTML Isotope JavaScript jQuery loop Map Menu Parallax PHP Rest API SASS SEO SQL Storefront SVG tab tag manager tags Taxonomy Tool upsell Webpack Wholesale WooCommerce WordPress WPML

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 / Terms of Service
© 2026 WP Flames - All Right Reserved