Category: WordPress

How to Update WPCS with Composer?

composer global require squizlabs/php_codesniffer
composer global require wp-coding-standards/wpcs
composer global require phpcompatibility/php-compatibility
composer global require dealerdirect/phpcodesniffer-composer-installer
phpcbf
phpcs

How to Show Admin Notice on Plugin Activation?

1. Add the code in the Notice Class like Below –

	/**
	 * Set transient only when the plugin is activated.
	 *
	 * @since 3.0.2
	 * @return void
	 */
	public static function add_the_transient() {
	    /* Create transient data */
	    set_transient( 'prefix-activation-notice', true, 5 );

	}
	/**
	 * Admin Notice on Activation.
	 *
	 * @since 3.0.2
	 * @return void
	 */
	public function add_activation_notice() {
		$itesm = get_posts( array( 'post_type' => 'my_post_type' ) );
		if ( count( $items ) > 0 ) {
			/* Check transient, if available display notice */
			if ( get_transient( 'prefix-activation-notice' ) ) {
				?>
				<div class="updated notice is-dismissible">
				<p>Thank you for using this plugin! <strong>You are awesome</strong>.</p>
				</div>
				<?php
				/* Delete transient, only display this notice once. */
				delete_transient( 'prefix-activation-notice' );
			}
		}
	}

Read More “How to Show Admin Notice on Plugin Activation?”

WPCS with Visual Studio Code

Installation

The easiest way to get started with PHP_CodeSniffer is to download the Phar files for each of the commands:
# Download using curl
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Or download using wget
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Then test the downloaded PHARs
php phpcs.phar -h
php phpcbf.phar -h

Composer Read More “WPCS with Visual Studio Code”

After Installation of Visual Studio Code

Go to Preferences  → Settings and then paste code within {} brackets –

"editor.minimap.enabled": true,
"files.autoSave": "afterDelay",
"editor.autoIndent": true,
"editor.detectIndentation": true,
"editor.fontSize": 14,
"editor.wordWrap": "on",

 

Thanks!

 

 

Change Custom Post Type Name Safely.

Just replace the second line’s post type names:

global $wpdb;
$old_post_types = array('old_type' => 'new_type');
foreach ($old_post_types as $old_type=>$type) {
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET post_type = REPLACE(post_type, %s, %s) 
                         WHERE post_type LIKE %s", $old_type, $type, $old_type ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET guid = REPLACE(guid, %s, %s) 
                         WHERE guid LIKE %s", "post_type={$old_type}", "post_type={$type}", "%post_type={$type}%" ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->posts} SET guid = REPLACE(guid, %s, %s) 
                         WHERE guid LIKE %s", "/{$old_type}/", "/{$type}/", "%/{$old_type}/%" ) );
}

 

Source: https://wordpress.stackexchange.com/questions/97026/how-do-i-safely-change-the-name-of-a-custom-post-type

Conditional Script Loading for Shotcode

In the functions.php file or in your plugin file write the code below –

[php]

/**
* Check if it should load frontend scripts
*
* @return mixed|void
*/
private function should_load_scripts() {
global $post;
$load_scripts = is_active_widget( false, false, ‘wpl_logo_carousel_pro_widget_content’, true ) || ( is_a( $post, ‘WP_Post’ ) && has_shortcode( $post->post_content, ‘logo-carousel-pro’ ) );

return apply_filters( ‘logo_carousel_pro_load_scripts’, $load_scripts );
}

/**
* Plugin Scripts and Styles
*
*/
function public_scripts() {
if ( $this->should_load_scripts() ) {
wp_enqueue_script( ‘jquery-isotope-min-js’, MY_URL . ‘assets/js/jquery.isotope.min.js’, array( ‘jquery’ ), $this->version, false );
}
}

[/php]

How to Set a default Title for the custom post?

[php]
function wpl_lc_add_custom_title( $data, $postarr ) {
if($data[‘post_type’] == ‘wpl_lcp_shortcodes’) {
if(empty($data[‘post_title’])) {
$data[‘post_title’] = ‘Untitled Carousel’;
}
}
return $data;
}
add_filter(‘wp_insert_post_data’, ‘wpl_lc_add_custom_title’, 10, 2 );
[/php]

Now use the function
[php]
the_title();
[/php]

Gray Scale with CSS with Cross Bowser Support

img {
filter: gray; /* IE6-9 */
-webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ & Opera 15+ */
filter: grayscale(1); /* Microsoft Edge and Firefox 35+ */
}

/* Disable grayscale on hover */
img:hover {
-webkit-filter: grayscale(0);
filter: none;
}

 

How to a Add Custom Meta Box for Multiple Post Type

[php]
add_action( ‘add_meta_boxes’, ‘myplugin_add_custom_box’ );

function myplugin_add_custom_box() {

add_meta_box( ‘myplugin_sectionid’, __( ‘My Post Section Title’, ‘myplugin_textdomain’ ), ‘myplugin_inner_custom_box’, ‘post’ );

add_meta_box( ‘myplugin_sectionid’, __( ‘My Post Section Title’, ‘myplugin_textdomain’ ), ‘myplugin_inner_custom_box’, ‘page’ );

}
[/php]

See: Stackexchange , Codex