Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Store custom field meta box data
*
* @param int $post_id The post ID.
*/
function food_save_meta_boxes_data( $post_id ){
// code here
}
add_action( 'save_post_food', 'food_save_meta_boxes_data', 10, 2 );
<?php
// verify meta box nonce
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
<?php
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
<?php
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
<?php
// store custom fields values
// cholesterol string
if ( isset( $_REQUEST['cholesterol'] ) ) {
update_post_meta( $post_id, '_food_cholesterol', sanitize_text_field( $_POST['cholesterol'] ) );
}
// store custom fields values
// carbohydrates string
if ( isset( $_REQUEST['carbohydrates'] ) ) {
<?php
/**
* Store custom field meta box data
*
* @param int $post_id The post ID.
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
*/
function food_save_meta_box_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
@carlodaniele
carlodaniele / food-example-plugin.php
Last active August 22, 2021 09:00
An example plugin for WPMU DEV readers
<?php
/**
* @package Food_example_plugin
* @version 1.0
*/
/*
Plugin Name: Food example plugin
Plugin URI: http://wordpress.org/extend/plugins/#
Description: This is an example plugin for WPMU DEV readers
Author: Carlo Daniele
@carlodaniele
carlodaniele / remove-toolbar.php
Last active February 27, 2016 06:23
Remove WordPress Toolbar for non-admin users
<?php
/**
* Remove WordPress Toolbar for subscribers
*/
function myplugin_remove_admin_bar() {
if ( ! current_user_can( 'publish_posts' ) ) {
show_admin_bar( false );
}
}
add_action( 'plugins_loaded', 'myplugin_remove_admin_bar' );
@carlodaniele
carlodaniele / remove-toolbar-2.php
Created February 18, 2016 16:28
Remove Toolbar for all users
<?php show_admin_bar( false ); ?>
@carlodaniele
carlodaniele / remove-toolbar-3.php
Last active February 18, 2016 19:16
Remove WordPress Toolbar for non-admin users
<?php
/**
* Remove WordPress Toolbar for all users except admins and editors
*
*/
function mytheme_remove_admin_bar() {
if ( ! current_user_can( 'publish_posts' ) ) {
show_admin_bar( false );
}
}