I am trying to create a separate download page in WordPress when the visitor clicks on a download button having link https://example.com/(any_post)/download, it will open a download page having the destination url.
What i have done?
I have created a custom field that takes link from post for this I uses below:
// Meta Box Class: DownloadLinkMetaBox
// Get the field value: $metavalue = get_post_meta( $post_id, $field_id, true );
class DownloadLinkMetaBox{
private $screen = array(
'post',
);
private $meta_fields = array(
array(
'label' => 'Download Link',
'id' => 'download_id',
'type' => 'text',
)
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_fields' ) );
}
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
'DownloadLink',
__( 'DownloadLink', '' ),
array( $this, 'meta_box_callback' ),
$single_screen,
'normal',
'default'
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'DownloadLink_data', 'DownloadLink_nonce' );
$this->field_generator( $post );
}
public function field_generator( $post ) {
$output = '';
foreach ( $this->meta_fields as $meta_field ) {
$label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
if ( empty( $meta_value ) ) {
if ( isset( $meta_field['default'] ) ) {
$meta_value = $meta_field['default'];
}
}
switch ( $meta_field['type'] ) {
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
$meta_field['id'],
$meta_field['id'],
$meta_field['type'],
$meta_value
);
}
$output .= $this->format_rows( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
public function format_rows( $label, $input ) {
return '<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';
}
public function save_fields( $post_id ) {
if ( ! isset( $_POST['DownloadLink_nonce'] ) )
return $post_id;
$nonce = $_POST['DownloadLink_nonce'];
if ( !wp_verify_nonce( $nonce, 'DownloadLink_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->meta_fields as $meta_field ) {
if ( isset( $_POST[ $meta_field['id'] ] ) ) {
switch ( $meta_field['type'] ) {
case 'email':
$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
break;
case 'text':
$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
break;
}
update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
} else if ( $meta_field['type'] === 'checkbox' ) {
update_post_meta( $post_id, $meta_field['id'], '0' );
}
}
}
}
if (class_exists('DownloadLinkMetabox')) {
new DownloadLinkMetabox;
};
That takes link perfectly and also able to show on front but not able to create another page.
Below code used to show in frontend
add_action( 'generate_after_entry_header', 'wpsh_single_posts_custom_meta_fields', 9 );
function wpsh_single_posts_custom_meta_fields(){
$post_id = get_the_ID();
$post = get_post( $post_id );
$download_id = get_post_meta( $post->ID, 'download_id' );
if( $download_id ) { ?>
<div class="wp-block-buttons aligncenter">
<div class="wp-block-button">
<a style='text-transform: uppercase;' href="<?php the_permalink();?>download/" class="wp-block-button__link has-vivid-cyan-blue-background-color download ripple">
<i class="material-icons">
<svg class="icon" fill="currentColor" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 0h24v24H0z" fill="none" />
<path d="M19.35 10.04C18.67 6.59 15.64 4 12 4 9.11 4 6.6 5.64 5.35 8.04 2.34 8.36 0 10.91 0 14c0 3.31 2.69 6 6 6h13c2.76 0 5-2.24 5-5 0-2.64-2.05-4.78-4.65-4.96zM17 13l-5 5-5-5h3V9h4v4h3z" />
</svg>
</i> Download</a>
</div>
</div>
<?php }
}
Above code will show button if the download id have link and the button have link example.com/any_post/download. Now here i don't know how to serve the link that the download_id holds of the post from it visits to .../download
I'm thinking to create a template page that serve the link but not able to do
in your functions:
Once you have added all your code make sure to flush your rewrite rules by going to: Settings > Permalinks and clicking 'save changes'
Set your rewrite rules:
function custom_add_rewrite_rule(){
$posts = get_posts( array( 'numberposts' => -1, 'post_type' => 'post') );
if( $posts ){
foreach($posts as $post ){
add_rewrite_rule(
$post->post_name . '/download/?$',
'index.php?name=' . $post->post_name . '&post_action=download&post_id=' . $post->ID,
'top'
);
}
}
}
add_action('init', 'custom_add_rewrite_rule');
add your query_vars:
function add_query_vars_filter( $vars ){
$vars[] = "post_action";
$vars[] = "post_id";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
then include your custom template:
function include_custom_template($template){
if( get_query_var('post_action') && get_query_var('post_action') == 'download'){
$template = get_template_directory() ."/download.php";
}
return $template;
}
add_filter('template_include', 'include_custom_template');
and finally make a file called download.php in your theme root and call your parameters
$download_id = get_post_meta( get_query_var('post_id'), 'download_id' );
//Your Code
Related
I use code that displays custom WooCommerce product tabs. This code is based on "Custom metabox content displayed in single product additional tabs on Woocommerce"
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Specifications Product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Technical Specification
echo '<h4>' . __( 'Technical Specification', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_technical_specification', true );
wp_editor( $value, '_technical_specification', array( 'editor_height' => 150 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_technical_specification' ]) )
update_post_meta( $post_id, '_technical_specification', wp_kses_post($_POST[ '_technical_specification' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $product;
// Technical Specification Tab
if ( ! empty($product) ) {
$tabs['technical_specification_tab'] = array(
'title' => __( 'Technical Specification', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_technical_specification_tab_content'
);
}
return $tabs;
}
function woo_technical_specification_tab_content() {
global $product;
echo'<p class="specification-text">'. $product->get_meta( '_technical_specification' ) . '</p>';
}
add_action('wp_footer', 'woo_activate_pdf_script');
function woo_activate_pdf_script(){
?>
<script>
var i, links = document.getElementsByTagName('a');
for(i=0; i<links.length; i++) {
if (links[i].href.match(/.pdf/ig)) links[i].className = 'pdf-icon';
}
</script>
<?php
}
Can you also check this code to see if it is correct? Tab visibility doesn't work here if there's no content in it.
And the speed of saving the product page when editing has increased.
I would be glad to have your help!
Try This
There is most likely a better way of doing this, but I've achieved this in the past with the following
if( get_field('directions') )
{
echo the_field('directions');
}
else
{
echo "<style>.direction_tab_tab { display:none !important; }</style>";
}
I used the answer from this question to create my custom product tabs:
Custom metabox content displayed in single product additional tabs on Woocommerce
Here's my code:
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Shipping
echo '<h4>' . __( 'Shipping', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_shipping', true );
wp_editor( $value, '_custom_shipping', array( 'editor_height' => 100 ) );
// Color
echo '<br><hr><h4>' . __( 'Color', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_color', true );
wp_editor( $value, '_custom_color', array( 'editor_height' => 100 ) );
// Material
echo '<br><hr><h4>' . __( 'Material', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_material', true );
wp_editor( $value, '_custom_material', array( 'editor_height' => 100 ) );
// Size
echo '<br><hr><h4>' . __( 'Size', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_size', true );
wp_editor( $value, '_custom_size', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_custom_shipping' ]) )
update_post_meta( $post_id, '_custom_shipping', wp_kses_post($_POST[ '_custom_shipping' ]) );
if( isset($_POST[ '_custom_color' ]) )
update_post_meta( $post_id, '_custom_color', wp_kses_post($_POST[ '_custom_color' ]) );
if( isset($_POST[ '_custom_material' ]) )
update_post_meta( $post_id, '_custom_material', wp_kses_post($_POST[ '_custom_material' ]) );
if( isset($_POST[ '_custom_size' ]) )
update_post_meta( $post_id, '_custom_size', wp_kses_post($_POST[ '_custom_size' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
// 1) Removing tabs
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
// 2 Adding new tabs and set the right order
//Adds Shipping Tab
$tabs['custom_shipping_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_custom_shipping_tab_content'
);
// Adds Color Tab
$tabs['custom_color_tab'] = array(
'title' => __( 'Color', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_custom_color_tab_content'
);
// Adds Material Tab
$tabs['custom_material_tab'] = array(
'title' => __( 'Material', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_custom_material_tab_content'
);
// Adds Size Tab
$tabs['custom_size_tab'] = array(
'title' => __( 'Size', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_custom_size_tab_content'
);
return $tabs;
}
function woo_custom_shipping_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_shipping' ) . '</p></div>';
}
function woo_custom_color_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_color' ) . '</p></div>';
}
function woo_custom_material_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_material' ) . '</p></div>';
}
function woo_custom_size_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_size' ) . '</p></div>';
}
Then I added custom code to create vertical tabs and added them after woocommerce_share hook.
/* Vertical Product Tabs */
add_action( 'woocommerce_share', 'woocommerce_output_product_data_tabs', 5 );
function woocommerce_output_product_data_tabs() {
$product_tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( empty( $product_tabs ) ) {
return;
}
foreach ( $product_tabs as $key => $product_tab ) {
echo '<div id="tab-' . esc_attr( $key ) . '">';
if ( isset( $product_tab[ 'callback' ] ) ) {
call_user_func( $product_tab[ 'callback' ], $key, $product_tab );
}
echo '</div>';
}
}
The result was as follows:
All the tabs have lost titles.
Now tabs are shown after woocommerce_share hook and in their old place. They are just copied.
Question:
How do I fix the code to show the tab titles?
How to fix the code so that the tabs are shown only after the woocommerce_share hook?
I would be glad to have your help!
First of all, I've slightly updated your existing code, to more recent version:
// You can use add_meta_boxes_{post_type} for best practice, so your hook will only run when editing a specific post type. This will only receive 1 parameter – $post
function action_add_meta_boxes_product( $post ) {
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
add_action( 'add_meta_boxes_product', 'action_add_meta_boxes_product', 10, 1 );
// Add custom metabox content
function additional_product_tabs_metabox_content( $post ) {
// Shipping
echo '<h4>' . __( 'Shipping', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_shipping', true );
wp_editor( $value, '_custom_shipping', array( 'editor_height' => 100 ) );
// Color
echo '<br><hr><h4>' . __( 'Color', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_color', true );
wp_editor( $value, '_custom_color', array( 'editor_height' => 100 ) );
// Material
echo '<br><hr><h4>' . __( 'Material', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_material', true );
wp_editor( $value, '_custom_material', array( 'editor_height' => 100 ) );
// Size
echo '<br><hr><h4>' . __( 'Size', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_custom_size', true );
wp_editor( $value, '_custom_size', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
function action_save_post_product( $post_id, $post, $update ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values
if ( isset( $_POST[ '_custom_shipping' ] ) )
update_post_meta( $post_id, '_custom_shipping', wp_kses_post( $_POST[ '_custom_shipping' ] ) );
if ( isset( $_POST[ '_custom_color' ] ) )
update_post_meta( $post_id, '_custom_color', wp_kses_post( $_POST[ '_custom_color' ] ) );
if ( isset( $_POST[ '_custom_material' ] ) )
update_post_meta( $post_id, '_custom_material', wp_kses_post( $_POST[ '_custom_material' ] ) );
if ( isset( $_POST[ '_custom_size' ] ) )
update_post_meta( $post_id, '_custom_size', wp_kses_post( $_POST[ '_custom_size' ] ) );
}
add_action( 'save_post_product', 'action_save_post_product', 10, 3 );
Then your biggest mistake is using $product_tabs = apply_filters( 'woocommerce_product_tabs', array() );. This is because not only your call is executed but also the default call of WooCommerce. That's why these are shown twice.
The solution is to simply add your custom tabs via an array that is separate from the default WooCommerce functionality.
So you get:
// Removing DEFAULT tabs
function filter_woocommerce_product_tabs( $tabs ) {
// Remove the description tab
unset( $tabs['description'] );
// Remove the additional information tab
unset( $tabs['additional_information'] );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
function my_custom_tabs() {
// Initialize
$tabs = array();
// Adds Shipping Tab
$tabs['custom_shipping_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_custom_shipping_tab_content'
);
// Adds Color Tab
$tabs['custom_color_tab'] = array(
'title' => __( 'Color', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_custom_color_tab_content'
);
// Adds Material Tab
$tabs['custom_material_tab'] = array(
'title' => __( 'Material', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_custom_material_tab_content'
);
// Adds Size Tab
$tabs['custom_size_tab'] = array(
'title' => __( 'Size', 'woocommerce' ),
'priority' => 40,
'callback' => 'woo_custom_size_tab_content'
);
return $tabs;
}
function woo_custom_shipping_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_shipping' ) . '</p></div>';
}
function woo_custom_color_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_color' ) . '</p></div>';
}
function woo_custom_material_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_material' ) . '</p></div>';
}
function woo_custom_size_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_custom_size' ) . '</p></div>';
}
function action_woocommerce_share() {
// Call your custom tabs
$product_tabs = my_custom_tabs();
// NOT empty
if ( ! empty( $product_tabs ) ) : ?>
<div class="woocommerce-tabs wc-tabs-wrapper">
<ul class="tabs wc-tabs" role="tablist">
<?php foreach ( $product_tabs as $key => $product_tab ) : ?>
<li class="<?php echo esc_attr( $key ); ?>_tab" id="tab-title-<?php echo esc_attr( $key ); ?>" role="tab" aria-controls="tab-<?php echo esc_attr( $key ); ?>">
<a href="#tab-<?php echo esc_attr( $key ); ?>">
<?php echo wp_kses_post( apply_filters( 'woocommerce_product_' . $key . '_tab_title', $product_tab['title'], $key ) ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php foreach ( $product_tabs as $key => $product_tab ) : ?>
<div class="woocommerce-Tabs-panel woocommerce-Tabs-panel--<?php echo esc_attr( $key ); ?> panel entry-content wc-tab" id="tab-<?php echo esc_attr( $key ); ?>" role="tabpanel" aria-labelledby="tab-title-<?php echo esc_attr( $key ); ?>">
<?php
if ( isset( $product_tab['callback'] ) ) {
call_user_func( $product_tab['callback'], $key, $product_tab );
}
?>
</div>
<?php endforeach; ?>
<?php do_action( 'woocommerce_product_after_tabs' ); ?>
</div>
<?php endif;
}
add_action( 'woocommerce_share', 'action_woocommerce_share' );
Note: displaying the tabs horizontally or vertically is then a matter of applying CSS, this is theme related. Optionally you can add extra classes to the output of these tabs.
I created this PHP code in WordPress to add image to the menu, but it is duplicating a div where there is an add button and I don't know why, I need it to be only one and not two results of the div, in case I only want the left div... I'll add an image about what I'm talking about so you can analyze my problem!
Structure is placed inside function wp_post_thumbnail_only_html
I left only the action that passes the button to facilitate.
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
class SNAP_Menu_Image{
public function __construct() {
add_action( 'admin_init', array( $this, 'admin_init' ), 99 );
}
public function admin_init() {
if ( !has_action( 'wp_nav_menu_item_custom_fields' ) ) {
}
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'menu_item_custom_fields' ), 10, 4 );
}
public function file_is_displayable_image( $result, $path ) {
if ( $result ) {
return true;
}
$fileExtension = pathinfo( $path, PATHINFO_EXTENSION );
return in_array( $fileExtension, $this->additionalDisplayableImageExtensions );
}
public function menu_image_init() {
add_post_type_support( 'nav_menu_item', array( 'thumbnail' ) );
$this->image_sizes = apply_filters( 'menu_image_default_sizes', $this->image_sizes );
if (is_array($this->image_sizes)) {
foreach ($this->image_sizes as $name => $params) {
add_image_size($name, $params[0], $params[1], ( array_key_exists(2, $params) ? $params[2] : false ) );
}
}
load_plugin_textdomain( 'menu-image', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
public function menu_image_nav_menu_manage_columns( $columns ) {
return $columns + array( 'image' => __( 'Image', 'menu-image' ) );
}
public function menu_image_save_post_action( $post_id, $post ) {
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position'
);
foreach ( $menu_image_settings as $setting_name ) {
if ( isset( $_POST[ $setting_name ][ $post_id ] ) && !empty( $_POST[ $setting_name ][ $post_id ] ) ) {
if ( $post->{"_$setting_name"} != $_POST[ $setting_name ][ $post_id ] ) {
update_post_meta( $post_id, "_$setting_name", esc_sql( $_POST[ $setting_name ][ $post_id ] ) );
}
}
}
}
public function wp_update_nav_menu_item_action( $item_menu_id, $menu_item_db_id ) {
global $sitepress, $icl_menus_sync;
if ( class_exists( 'SitePress' ) && $sitepress instanceof SitePress && class_exists( 'ICLMenusSync' ) && $icl_menus_sync instanceof ICLMenusSync ) {
static $run_times = array();
$menu_image_settings = array(
'menu_item_image_size',
'menu_item_image_title_position',
'thumbnail_id',
'thumbnail_hover_id',
);
foreach ( $icl_menus_sync->menus as $menu_id => $menu_data ) {
if ( !isset( $_POST[ 'sync' ][ 'add' ][ $menu_id ] ) ) {
continue;
}
$cache_key = md5( serialize( array( $item_menu_id, 'tax_nav_menu' ) ) );
$cache_group = 'get_language_for_element';
wp_cache_delete( $cache_key, $cache_group );
$lang = $sitepress->get_language_for_element( $item_menu_id, 'tax_nav_menu' );
if ( !isset( $run_times[ $menu_id ][ $lang ] ) ) {
$run_times[ $menu_id ][ $lang ] = 0;
}
$post_item_ids = array();
foreach ( $_POST[ 'sync' ][ 'add' ][ $menu_id ] as $id => $lang_array ) {
if ( array_key_exists( $lang, $lang_array ) ) {
$post_item_ids[ ] = $id;
}
}
if ( !array_key_exists( $run_times[ $menu_id ][ $lang ], $post_item_ids ) ) {
continue;
}
$orig_item_id = $post_item_ids[ $run_times[ $menu_id ][ $lang ] ];
$orig_item_meta = get_metadata( 'post', $orig_item_id );
foreach ( $menu_image_settings as $meta ) {
if ( isset( $orig_item_meta[ "_$meta" ] ) && isset( $orig_item_meta[ "_$meta" ][ 0 ] ) ) {
update_post_meta( $menu_item_db_id, "_$meta", $orig_item_meta[ "_$meta" ][ 0 ] );
}
}
$run_times[ $menu_id ][ $lang ] ++;
break;
}
}
}
public function menu_image_edit_nav_menu_walker_filter() {
return 'Menu_Image_Walker_Nav_Menu_Edit';
}
public function menu_image_wp_setup_nav_menu_item( $item ) {
if ( !isset( $item->thumbnail_id ) ) {
$item->thumbnail_id = get_post_thumbnail_id( $item->ID );
}
if ( !isset( $item->thumbnail_hover_id ) ) {
$item->thumbnail_hover_id = get_post_meta( $item->ID, '_thumbnail_hover_id', true );
}
if ( !isset( $item->image_size ) ) {
$item->image_size = get_post_meta( $item->ID, '_menu_item_image_size', true );
}
if ( !isset( $item->title_position ) ) {
$item->title_position = get_post_meta( $item->ID, '_menu_item_image_title_position', true );
}
return $item;
}
public function menu_image_nav_menu_link_attributes_filter( $atts, $item, $args, $depth = null ) {
$this->setProcessed( $item->ID );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = ! empty( $atts[ 'class' ] ) ? $atts[ 'class' ] : '';
$class .= " menu-image-title-{$position}";
if ( $item->thumbnail_hover_id ) {
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$class .= ' menu-image-not-hovered';
}
if ( ! empty( $args->walker ) && class_exists( 'FlatsomeNavDropdown' ) && $args->walker instanceof FlatsomeNavDropdown && ! is_null( $depth ) && $depth === 0 ) {
$class .= ' nav-top-link';
}
$atts[ 'class' ] = trim( $class );
return $atts;
}
public function menu_image_nav_menu_item_title_filter( $title, $item, $depth = null, $args = null) {
if (is_numeric($item)) {
$item = wp_setup_nav_menu_item( get_post( $item ) );
}
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
}
$none = ''; // Sugar.
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $title, $image );
break;
case 'after':
default:
$item_args = array( $image, $title, $none );
break;
}
$title = vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
return $title;
}
public function menu_image_nav_menu_item_filter( $item_output, $item, $depth, $args ) {
if ( $this->isProcessed( $item->ID ) ) {
return $item_output;
}
$attributes = !empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
$attributes .= !empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
$attributes .= !empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
$attributes .= !empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
$attributes_array = shortcode_parse_atts($attributes);
$image_size = $item->image_size ? $item->image_size : apply_filters( 'menu_image_default_size', 'menu-36x36' );
$position = $item->title_position ? $item->title_position : apply_filters( 'menu_image_default_title_position', 'after' );
$class = "menu-image-title-{$position}";
$this->setUsedAttachments( $image_size, $item->thumbnail_id );
$image = '';
if ( $item->thumbnail_hover_id ) {
$this->setUsedAttachments( $image_size, $item->thumbnail_hover_id );
$hover_image_src = wp_get_attachment_image_src( $item->thumbnail_hover_id, $image_size );
$margin_size = $hover_image_src[ 1 ];
$image = "<span class='menu-image-hover-wrapper'>";
$image .= wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$image .= wp_get_attachment_image(
$item->thumbnail_hover_id, $image_size, false, array(
'class' => "hovered-image {$class}",
'style' => "margin-left: -{$margin_size}px;",
)
);
$image .= '</span>';;
$class .= ' menu-image-hovered';
} elseif ( $item->thumbnail_id ) {
$image = wp_get_attachment_image( $item->thumbnail_id, $image_size, false, "class=menu-image {$class}" );
$class .= ' menu-image-not-hovered';
}
$attributes_array['class'] = $class;
$attributes_array = apply_filters( 'menu_image_link_attributes', $attributes_array, $item, $depth, $args );
$attributes = '';
foreach ( $attributes_array as $attr_name => $attr_value ) {
$attributes .= "{$attr_name}=\"$attr_value\" ";
}
$attributes = trim($attributes);
$item_output = "{$args->before}<a {$attributes}>";
$link = $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$none = '';
switch ( $position ) {
case 'hide':
case 'before':
case 'above':
$item_args = array( $none, $link, $image );
break;
case 'after':
default:
$item_args = array( $image, $link, $none );
break;
}
$item_output .= vsprintf( '%s<span class="menu-image-title">%s</span>%s', $item_args );
$item_output .= "</a>{$args->after}";
return $item_output;
}
public function menu_image_add_inline_style_action() {
wp_register_style( 'menu-image', SF()->plugin_url() . '/assets/css/menu-image.css', array(), '1.1', 'all' );
wp_enqueue_style( 'menu-image' );
}
public function menu_image_admin_head_nav_menus_action() {
wp_enqueue_script( 'menu-image-admin', SF()->plugin_url() . '/assets/js/menu-image.js', array( 'jquery' ) );
wp_localize_script(
'menu-image-admin', 'menuImage', array(
'l10n' => array(
'uploaderTitle' => __( 'Chose menu image', 'menu-image' ),
'uploaderButtonText' => __( 'Select', 'menu-image' ),
),
'settings' => array(
'nonce' => wp_create_nonce( 'update-menu-item' ),
),
)
);
wp_enqueue_media();
wp_enqueue_style( 'editor-buttons' );
}
public function menu_image_delete_menu_item_image_action() {
$menu_item_id = (int) $_REQUEST[ 'menu-item' ];
check_admin_referer( 'delete-menu_item_image_' . $menu_item_id );
if ( is_nav_menu_item( $menu_item_id ) && has_post_thumbnail( $menu_item_id ) ) {
delete_post_thumbnail( $menu_item_id );
delete_post_meta( $menu_item_id, '_thumbnail_hover_id' );
delete_post_meta( $menu_item_id, '_menu_item_image_size' );
delete_post_meta( $menu_item_id, '_menu_item_image_title_position' );
}
}
public function wp_post_thumbnail_only_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$markup = '<p class="description description-thin" ><label>%s<br /><a title="%s" href="#" class="set-post-thumbnail button%s" data-item-id="%s" style="height: auto;">%s</a>%s</label></p>';
$thumbnail_id = get_post_thumbnail_id( $item_id );
$content = sprintf(
$markup,
esc_html__( 'Menu image', 'menu-image' ),
$thumbnail_id ? esc_attr__( 'Change menu item image', 'menu-image' ) : esc_attr__( 'Set menu item image', 'menu-image' ),
'',
$item_id,
$thumbnail_id ? wp_get_attachment_image( $thumbnail_id, $default_size ) : esc_html__( 'Set image', 'menu-image' ),
$thumbnail_id ? '' . __( 'Remove', 'menu-image' ) . '' : ''
);
return $content;
}
public function wp_post_thumbnail_html( $item_id ) {
$default_size = apply_filters( 'menu_image_default_size', 'menu-36x36' );
$content = $this->wp_post_thumbnail_only_html( $item_id );
$image_size = get_post_meta( $item_id, '_menu_item_image_size', true );
if ( !$image_size ) {
$image_size = $default_size;
}
$title_position = get_post_meta( $item_id, '_menu_item_image_title_position', true );
if ( !$title_position ) {
$title_position = apply_filters( 'menu_image_default_title_position', 'after' );
}
$content = "<div class='menu-item-images' style='min-height:70px'>$content</div>";
return apply_filters( 'admin_menu_item_thumbnail_html', $content, $item_id );
}
public function wp_ajax_set_menu_item_thumbnail() {
$json = !empty( $_REQUEST[ 'json' ] );
$post_ID = intval( $_POST[ 'post_id' ] );
if ( !current_user_can( 'edit_post', $post_ID ) ) {
wp_die( - 1 );
}
$thumbnail_id = intval( $_POST[ 'thumbnail_id' ] );
$is_hovered = (bool) $_POST[ 'is_hover' ];
check_ajax_referer( "update-menu-item" );
if ( $thumbnail_id == '-1' ) {
if ( $is_hovered ) {
$success = delete_post_meta( $post_ID, '_thumbnail_hover_id' );
} else {
$success = delete_post_thumbnail( $post_ID );
}
} else {
if ( $is_hovered ) {
$success = update_post_meta( $post_ID, '_thumbnail_hover_id', $thumbnail_id );
} else {
$success = set_post_thumbnail( $post_ID, $thumbnail_id );
}
}
if ( $success ) {
$return = $this->wp_post_thumbnail_only_html( $post_ID );
$json ? wp_send_json_success( $return ) : wp_die( $return );
}
wp_die( 0 );
}
public function menu_item_custom_fields( $item_id, $item, $depth, $args ){
?>
<div class="field-image hide-if-no-js wp-media-buttons">
<?php echo $this->wp_post_thumbnail_html( $item_id ) ?>
</div>
<?php
}
public function jetpack_photon_override_image_downsize_filter( $prevent, $data ) {
return $this->isAttachmentUsed( $data[ 'attachment_id' ], $data[ 'size' ] );
}
public function setUsedAttachments( $size, $id ) {
$this->used_attachments[ $size ][ ] = $id;
}
public function isAttachmentUsed( $id, $size = null ) {
if ( ! is_null( $size ) ) {
return is_string( $size ) && isset( $this->used_attachments[ $size ] ) && in_array( $id, $this->used_attachments[ $size ] );
} else {
foreach ( $this->used_attachments as $used_attachment ) {
if ( in_array( $id, $used_attachment ) ) {
return true;
}
}
return false;
}
}
public function wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
if ( $this->isAttachmentUsed( $attachment->ID, $size ) ) {
unset( $attr['sizes'], $attr['srcset'] );
}
return $attr;
}
protected function setProcessed( $id ) {
$this->processed[] = $id;
}
protected function isProcessed( $id ) {
return in_array( $id, $this->processed );
}
}
$menu_image = new SNAP_Menu_Image();
require_once(ABSPATH . 'wp-admin/includes/nav-menu.php');
class Menu_Image_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
ob_start();
$item_id = esc_attr( $item->ID );
$removed_args = array(
'action',
'customlink-tab',
'edit-menu-item',
'menu-item',
'page-tab',
'_wpnonce',
);
$original_title = false;
if ( 'taxonomy' == $item->type ) {
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
if ( is_wp_error( $original_title ) )
$original_title = false;
} elseif ( 'post_type' == $item->type ) {
$original_object = get_post( $item->object_id );
$original_title = get_the_title( $original_object->ID );
} elseif ( 'post_type_archive' == $item->type ) {
$original_object = get_post_type_object( $item->object );
if ( $original_object ) {
$original_title = $original_object->labels->archives;
}
}
$classes = array(
'menu-item menu-item-depth-' . $depth,
'menu-item-' . esc_attr( $item->object ),
'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
);
$title = $item->title;
if ( ! empty( $item->_invalid ) ) {
$classes[] = 'menu-item-invalid';
$title = sprintf( __( '%s (Invalid)' ), $item->title );
} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
$classes[] = 'pending';
$title = sprintf( __('%s (Pending)'), $item->title );
}
$title = ( ! isset( $item->label ) || '' == $item->label ) ? $title : $item->label;
$submenu_text = '';
if ( 0 == $depth )
$submenu_text = 'style="display: none;"';
?>
<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
<div class="menu-item-bar">
<div class="menu-item-handle">
<span class="item-title"><span class="menu-item-title"><?php echo esc_html( $title ); ?></span> <span class="is-submenu" <?php echo $submenu_text; ?>><?php _e( 'sub item' ); ?></span></span>
<span class="item-controls">
<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
<span class="item-order hide-if-js">
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-up-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-up" aria-label="<?php esc_attr_e( 'Move up' ) ?>">↑</a>
|
<a href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'move-down-menu-item',
'menu-item' => $item_id,
),
remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
),
'move-menu_item'
);
?>" class="item-move-down" aria-label="<?php esc_attr_e( 'Move down' ) ?>">↓</a>
</span>
<a class="item-edit" id="edit-<?php echo $item_id; ?>" href="<?php
echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
?>" aria-label="<?php esc_attr_e( 'Edit menu item' ); ?>"><span class="screen-reader-text"><?php _e( 'Edit' ); ?></span></a>
</span>
</div>
</div>
<div class="menu-item-settings wp-clearfix" id="menu-item-settings-<?php echo $item_id; ?>">
<?php if ( 'custom' == $item->type ) : ?>
<p class="field-url description description-wide">
<label for="edit-menu-item-url-<?php echo $item_id; ?>">
<?php _e( 'URL' ); ?><br />
<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
</label>
</p>
<?php endif; ?>
<p class="field-xfn description description-thin">
<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
<?php _e( 'Link Relationship (XFN)' ); ?><br />
<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
</label>
</p>
<?php
do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
?>
<fieldset class="field-move hide-if-no-js description description-wide">
<span class="field-move-visual-label" aria-hidden="true"><?php _e( 'Move' ); ?></span>
<button type="button" class="button-link menus-move menus-move-up" data-dir="up"><?php _e( 'Up one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-down" data-dir="down"><?php _e( 'Down one' ); ?></button>
<button type="button" class="button-link menus-move menus-move-left" data-dir="left"></button>
<button type="button" class="button-link menus-move menus-move-right" data-dir="right"></button>
<button type="button" class="button-link menus-move menus-move-top" data-dir="top"><?php _e( 'To the top' ); ?></button>
</fieldset>
<div class="menu-item-actions description-wide submitbox">
<?php if ( 'custom' != $item->type && $original_title !== false ) : ?>
<p class="link-to-original">
<?php printf( __('Original: %s'), '' . esc_html( $original_title ) . '' ); ?>
</p>
<?php endif; ?>
<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
echo wp_nonce_url(
add_query_arg(
array(
'action' => 'delete-menu-item',
'menu-item' => $item_id,
),
admin_url( 'nav-menus.php' )
),
'delete-menu_item_' . $item_id
); ?>"><?php _e( 'Remove' ); ?></a> <span class="meta-sep hide-if-no-js"> | </span> <a class="item-cancel submitcancel hide-if-no-js" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array( 'edit-menu-item' => $item_id, 'cancel' => time() ), admin_url( 'nav-menus.php' ) ) );
?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
</div>
<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
</div>
<ul class="menu-item-transport"></ul>
<?php
$output .= ob_get_clean();
}
}
As basic as the answer is, I'll leave the question active...
I had instantiated the class 2 times!
I have created a couple of custom post type. To one of them i also want to add a custom field. It should just be a simple textfield where you can enter some text. Similar to the title field. How would you do that? I dont want to use a plugin.
Current code (functions.php)
register_post_type( 'cases',
array(
'labels' => array(
'name' => __( 'Cases' ),
'singular_name' => __( 'Case' )
),
'publicly_queryable' => true,
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'cases'),
'supports' => array('title','editor','thumbnail')
)
);
You need to create custom meta box and add that field within metabox.
Create Metabox
function add_your_fields_meta_box() {
add_meta_box(
'your_fields_meta_box', // $id
'Your Fields', // $title
'show_your_fields_meta_box', // $callback
'your_post', // $screen
'normal', // $context
'high' // $priority
);
}
add_action( 'add_meta_boxes', 'add_your_fields_meta_box' );
Html part
function show_your_fields_meta_box() {
global $post;
$meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
<!-- All fields will go here -->
<?php }
Save field in database
function save_your_fields_meta( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'your_fields', true );
$new = $_POST['your_fields'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'your_fields', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'your_fields', $old );
}
}
add_action( 'save_post', 'save_your_fields_meta' );
For more detail you can check here https://www.taniarascia.com/wordpress-part-three-custom-fields-and-metaboxes/, This is very nice link which will help you to create custom meta box and field step by step
I know that this is way late, but under your function, you need to add 'custom-fields' to your support:
'supports' => array('title','editor','thumbnail', 'custom-fields'
If you want to create custom fields, you have to add metabox.
See my example that will help you.
function show_your_fields_meta_box() {
global $post;
$meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
// Just paste your input here as below.
<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
<p>
<label for="your_fields[text]">Input Text</label>
<br>
<input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="<?php echo $meta['text']; ?>">
</p>
<?php }
function save_your_fields_meta( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'your_fields', true );
$new = $_POST['your_fields'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'your_fields', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'your_fields', $old );
}
}
add_action( 'save_post', 'save_your_fields_meta' );
?>
I have been trying to implement a TinyMCE text area for adding and editing a post meta item, below is the code I tried.
I am able to generate the TinyMCE editor in post edit form but not able to load the post meta data from DB, can anyone please help me? Thanks in advance.
function target_audience_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, '_target_audience', true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function target_audience_add_meta_box() {
add_meta_box(
'target_audience',
__( 'Target Audience', 'target_audience' ),
'target_audience_html',
'product',
'normal',
'default'
);
}
function target_audience_html(){
wp_nonce_field( '_target_audience_nonce', 'target_audience_nonce' );
$target_audience = get_post_meta($post->ID, '_target_audience', true);
wp_editor( $target_audience, '_target_audience', array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'target_audience',
'textarea_rows' => 10,
'teeny' => true
) );
}
function target_audience_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['target_audience'] ) || ! wp_verify_nonce( $_POST['target_audience_nonce'], '_target_audience_nonce' ) ) return;
if ( isset( $_POST['target_audience'] ) )
update_post_meta( $post_id, '_target_audience', esc_attr( $_POST['target_audience'] ) );
}
add_action( 'save_post', 'target_audience_save' );
target_audience_get_meta('_target_audience');
The problem is very simple. I just forgot to input the $post parameter to the function target_audience_html($post). Now it is working.