October 6, 201213 yr I just found out that my clients server uses php 5.2.17 and I have anonymous functions all over the place. I know I have to name them but apparently I'm doing it all wrong as its not working and I'm getting a t_function error. function add_post_type($name, $args= array()) { add_action('init', function() use($name, $args) { $upper = ucwords($name); $name = strtolower(str_replace(' ', '_', $name)); $args = array_merge( array( 'public' => true, 'label' => "$upper" . 's', 'labels' => array('add_new_item' => "Add New $upper"), 'rewrite' => array( 'slug' => 'books' ), 'query_var' => true, 'supports' => array('title', 'editor', 'comments', 'excerpt', 'thumbnail'), 'capability_type'=> 'page', 'hierarchical' => true ), $args ); register_post_type( $name, $args ); }); } I tried naming the function in the second line and creating a callback function but apparently I'm doing it wrong. Any help would be much appreciated.
October 6, 201213 yr I just found out that my clients server uses php 5.2.17 and I have anonymous functions all over the place. I know I have to name them but apparently I'm doing it all wrong as its not working and I'm getting a t_function error. function add_post_type($name, $args= array()) { add_action('init', function() use($name, $args) { $upper = ucwords($name); $name = strtolower(str_replace(' ', '_', $name)); $args = array_merge( array( 'public' => true, 'label' => "$upper" . 's', 'labels' => array('add_new_item' => "Add New $upper"), 'rewrite' => array( 'slug' => 'books' ), 'query_var' => true, 'supports' => array('title', 'editor', 'comments', 'excerpt', 'thumbnail'), 'capability_type'=> 'page', 'hierarchical' => true ), $args ); register_post_type( $name, $args ); }); } I tried naming the function in the second line and creating a callback function but apparently I'm doing it wrong. Any help would be much appreciated. what exactly are u trying to do in wordpress?
October 6, 201213 yr Author A custom post type with a meta box that inserts an associated url. That was only a piece of it. Here's the whole thing: function add_post_type($name, $args= array()) { add_action('init', function() use($name, $args) { $upper = ucwords($name); $name = strtolower(str_replace(' ', '_', $name)); $args = array_merge( array( 'public' => true, 'label' => "$upper" . 's', 'labels' => array('add_new_item' => "Add New $upper"), 'rewrite' => array( 'slug' => 'books' ), 'query_var' => true, 'supports' => array('title', 'editor', 'comments', 'excerpt', 'thumbnail'), 'capability_type'=> 'page', 'hierarchical' => true ), $args ); register_post_type( $name, $args ); }); } add_post_type('book', array()); // Create out post type add_action( 'init', 'create_post_type' ); function create_post_type() { $args = array( 'labels' => post_type_labels( 'Book' ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } // A helper function for generating the labels function post_type_labels( $singular, $plural = '' ) { if( $plural == '') $plural = $singular .'s'; return array( 'name' => _x( $plural, 'post type general name' ), 'singular_name' => _x( $singular, 'post type singular name' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New '. $singular ), 'edit_item' => __( 'Edit '. $singular ), 'new_item' => __( 'New '. $singular ), 'view_item' => __( 'View '. $singular ), 'search_items' => __( 'Search '. $plural ), 'not_found' => __( 'No '. $plural .' found' ), 'not_found_in_trash' => __( 'No '. $plural .' found in Trash' ), 'parent_item_colon' => '' ); } //add filter to ensure the text Book, or book, is displayed when user updates a book add_filter('post_updated_messages', 'post_type_updated_messages'); function post_type_updated_messages( $messages ) { global $post, $post_ID; $messages['book'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Book updated.'), /* translators: %s: date and time of the revision */ 5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ), 7 => __('Book saved.'), 8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), 9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'), // translators: Publish box date format, see php.net/date date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), 10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), ); return $messages; } add_action('add_meta_boxes', function() { add_meta_box( 'kc_buy_now', 'Buy Now link', 'kc_buy_now_cb', 'book', 'normal', 'high' #,$args ); }); function kc_buy_now_cb() { global $post; $url = get_post_meta($post->ID, 'buy_now_associated_url', true); //unique identifier, name of hidden field wp_nonce_field(__FILE__, 'kc_nonce'); ?> <label for="buy_now_associated_url">Associated URL:</label> <input type="text" name="buy_now_associated_url" id="buy_now_associated_url" class="widefat" value="" /><?php echo $url; } add_action('save_post', function() { global $post; if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; //security check nonce if($_POST && !wp_verify_nonce($_POST['kc_nonce'], __FILE__) ) { return; } if (isset($_POST['buy_now_associated_url']) ) { update_post_meta($post->ID, 'buy_now_associated_url', $_POST['buy_now_associated_url']); } }); Its a bit of a muddle as I was trying to learn as I went along but it was working locally. Edited October 6, 201213 yr by mantis
Create an account or sign in to comment