Wordpress theme options text area not displaying text at front end - wordpress

I am new to the PHP and I am trying to create a theme options in wordpress. Rest of the the theme options are working fine but the option 3 which I had created is not showing the text at front end
<?php
//register settings
function theme_settings_init(){
register_setting( 'theme_settings', 'theme_settings' );
}
//add settings page to menu
function add_settings_page() {
add_menu_page( __( 'Theme Settings' ), __( 'Theme Settings' ), 'manage_options', 'settings', 'theme_settings_page');
}
//add actions
add_action( 'admin_init', 'theme_settings_init' );
add_action( 'admin_menu', 'add_settings_page' );
//define your variables
$color_scheme = array('default','blue','green',);
//start settings page
function theme_settings_page() {
if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;
//get variables outside scope
global $color_scheme;
?>
<div>
<div id="icon-options-general"></div>
<h2><?php _e( 'Elegant Theme Settings' ) //your admin panel title ?></h2>
<?php
//show saved options message
if ( false !== $_REQUEST['updated'] ) : ?>
<div><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'theme_settings' ); ?>
<?php $options = get_option( 'theme_settings' ); ?>
<table>
<!-- Option 1: Custom Logo -->
<tr valign="top">
<th scope="row"><?php _e( 'Custom Logo' ); ?></th>
<td><input id="theme_settings[custom_logo]" type="text" size="36" name="theme_settings[custom_logo]" value="<?php esc_attr_e( $options['custom_logo'] ); ?>" />
<label for="theme_settings[custom_logo]"><?php _e( 'Enter the URL to your custom logo' ); ?></label></td>
</tr>
<!-- Option 2: Color Scheme -->
<tr valign="top">
<th scope="row"><?php _e( 'Color Scheme' ); ?></th>
<td><select name="theme_settings[color_scheme]">
<?php foreach ($color_scheme as $option) { ?>
<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
<?php } ?>
</select>
<label for="theme_settings[color_scheme]"><?php _e( 'Choose Your Color Scheme' ); ?></label></td>
</tr>
<!-- Option 3: Intro Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Intro' ); ?></th>
<td><label for="theme_settings[headline]"><?php _e( 'Enter your Intro Headline' ); ?></label>
<br />
<textarea id="theme_settings[headline]" name="theme_settings[headline]" rows="5" cols="36"><?php esc_attr_e( $options['headline'] ); ?></textarea></td>
</tr>
<!-- Option 4: Tracking Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Tracking Code' ); ?></th>
<td><label for="theme_settings[tracking]"><?php _e( 'Enter your analytics tracking code' ); ?></label>
<br />
<textarea id="theme_settings[tracking]" name="theme_settings[tracking]" rows="5" cols="36"><?php esc_attr_e( $options['tracking'] ); ?></textarea></td>
</tr>
</table>
<p><input name="submit" id="submit" value="Save Changes" type="submit"></p>
</form>
</div><!-- END wrap -->
<?php
}
//sanitize and validate
function options_validate( $input ) {
global $select_options, $radio_options;
if ( ! isset( $input['option'] ) )
$input['option'] = null;
$input['option'] = ( $input['option'] == 1 ? 1 : 0 );
$input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
if ( ! isset( $input['radioinput'] ) )
$input['radioinput'] = null;
if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
$input['radioinput'] = null;
$input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
return $input;
}
?>
All the other options are working fine but when I am adding the text box in the intro it is not displaying the content on the front end
<?php //show tracking code for the header
echo $options['headline'];?>
Please help me

You are using the $options directly in the second option.
<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
I think it is overwriting your second option.

Related

Data is not saving to options table from cutsom post type

I have a custom plugin which create a custom post type on activation. I have created one sub menu page under that custom post type.My codes worked fine upto that, now am trying to save from fields to options table.But data is not saving to table. Please find the codes below.
I followed this tutorial https://tommcfarlin.com/save-wordpress-submenu-page-options/
The above tutorial works fine if i create a sub menu page under 'Tools' menu, but it is not working for page cretaed under custom post type. Please advice me what are the changes required.
Codes For Creating Sub menu
function alumns_reps_submenu()
{
add_submenu_page(
'edit.php?post_type=alumns-reps',
__( 'Settings', 'alumns-reps-settings' ),
__( 'Settings', 'alumns-reps-settings' ),
'manage_options',
'alumns-settings',
'alumns_reps_settings_callback'
);
}
Codes For View
function alumns_reps_settings_callback(){
?>
<?php
/**
* Renders the content of the submenu page for the Reps-Alumns Settings page.
*
*
*/
$settings_title = __( 'General Settings' );
?>
<div class="wrap">
<h1><?php echo esc_html( $settings_title ); ?></h1>
<form method="post" action="">
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="state-reps-headding"><?php _e( 'Headding' ); ?></label></th>
<td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding') ?>" class="regular-text" /></td>
</tr>
<tr>
<th scope="row"><label for="state-reps-text"><?php _e( 'State Reps Title' ); ?></label></th>
<td><input name="state-reps-text" type="text" id="state-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('state-reps-text') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="current-reps-tab-title"><?php _e( 'Current Rep Tab Title' ); ?></label></th>
<td><input name="current-reps-tab-title" type="text" id="current-reps-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-tab-title') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="alumini-tab-title"><?php _e( 'Alumini Tab Title' ); ?></label></th>
<td><input name="alumini-tab-title" type="text" id="alumini-tab-title" aria-describedby="tagline-description" value="<?php echo get_option('alumini-tab-title') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><label for="current-reps-text"><?php _e( 'Current Reps Title' ); ?></label></th>
<td><input name="current-reps-text" type="text" id="current-reps-text" aria-describedby="tagline-description" value="<?php echo get_option('current-reps-text') ?>" class="regular-text" />
</td>
</tr>
<tr>
<th scope="row"><?php _e( 'No Current Reps' ); ?></th>
<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No Current Reps' ); ?></span></legend>
<textarea name="no-current-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-current-reps-text') ); ?></textarea>
</p>
</fieldset></td>
</tr>
<tr>
<th scope="row"><?php _e( 'No State Reps' ); ?></th>
<td><fieldset><legend class="screen-reader-text"><span><?php _e( 'No State Reps' ); ?></span></legend>
<textarea name="no-state-reps-text" rows="5" cols="50" id="no-current-reps-text" class="large-text code"><?php echo esc_textarea( get_option('no-state-reps-text') ); ?></textarea>
</p>
</fieldset></td>
</tr>
</table>
<?php submit_button(); ?>
<?php wp_nonce_field( 'alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce' ); ?>
</form>
</div><!-- .wrap -->
<?php
}
Codes For saving data to options table
add_action( 'load-alumns-settings', 'alumns_reps_save_settings' );
/**
* The method for saving the options to the database or for deleting them
* based on what the user has specified on the settings page.
*
*/
function alumns_reps_save_settings() {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
// If the user doesn't have permission to save, then display an error message
if ( ! alums_reps_user_can_save( $action, $nonce ) ) {
return;
}
if ( isset( $_POST['state-reps-headding'] ) ) {
$main_title = $_POST['state-reps-headding'];
update_option( 'state-reps-main-headding', $main_title);
} else {
delete_option( 'state-reps-headding' );
}
if ( isset( $_POST['state-reps-text'] ) ) {
$state_reps_text = $_POST['state-reps-text'];
update_option( 'state-reps-text', $state_reps_text);
} else {
delete_option( 'state-reps-text' );
}
if ( isset( $_POST['current-reps-tab-title'] ) ) {
$current_reps_tab_title = $_POST['current-reps-tab-title'];
update_option( 'current-reps-tab-title', $current_reps_tab_title);
} else {
delete_option( 'current-reps-tab-title' );
}
if ( isset( $_POST['alumini-tab-title'] ) ) {
$alumini_tab_title = $_POST['alumini-tab-title'];
update_option( 'alumini-tab-title', $alumini_tab_title);
} else {
delete_option( 'alumini-tab-title' );
}
if ( isset( $_POST['current-reps-text'] ) ) {
$current_reps_text = $_POST['current-reps-text'];
update_option( 'current-reps-text', $current_reps_text);
} else {
delete_option( 'current-reps-text' );
}
if ( isset( $_POST['no-current-reps-text'] ) ) {
$no_current_reps_text = $_POST['no-current-reps-text'];
update_option( 'no-current-reps-text', $no_current_reps_text);
} else {
delete_option( 'no-current-reps-text' );
}
if ( isset( $_POST['no-state-reps-text'] ) ) {
$no_state_reps_text = $_POST['no-state-reps-text'];
update_option( 'no-state-reps-text', $no_state_reps_text);
} else {
delete_option( 'no-state-reps-text' );
}
}
/**
* Determines if the user has permission to save the information from the settings page
*
*
* #return bool True if the user has permission to save; false, otherwise.
*/
function alums_reps_user_can_save( $action, $nonce ) {
$is_nonce_set = isset( $_POST[ $nonce ] );
$is_valid_nonce = false;
if ( $is_nonce_set ) {
$is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
}
return ( $is_nonce_set && $is_valid_nonce );
}
You are using the wrong hook on this line:
add_action( "load-{$page_hook}", 'alumns_reps_save_settings' );
The plugin Query Monitor is a must-have to a developer. You need the screen ID:
So, your load-{$page_hook} action should be:
add_action('load-alumns-reps_page_alumns-settings', function () {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
if (!alums_reps_user_can_save($action, $nonce)) {
return;
}
// DO YOUR THING
});
This is the self-contained code I used for testing. Note the wp_die() to debug the save action.
/* Required a CPT with slug "alumns-reps" */
add_action('admin_menu', function () {
add_submenu_page(
'edit.php?post_type=alumns-reps',
__('Settings', 'alumns-reps-settings'),
__('Settings', 'alumns-reps-settings'),
'manage_options',
'alumns-settings',
'alumns_reps_settings_callback'
);
});
function alumns_reps_settings_callback() {
$settings_title = __('General Settings');
?>
<div class="wrap">
<h1><?php echo esc_html($settings_title); ?></h1>
<form method="post" action="">
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="state-reps-headding"><?php _e('Headding'); ?></label></th>
<td><input name="state-reps-headding" type="text" id="state-reps-headding" value="<?php echo get_option('state-reps-main-headding') ?>" class="regular-text" /></td>
</tr>
</table>
<?php submit_button(); ?>
<?php wp_nonce_field('alumns-reps-settings-page-save', 'alumns-reps-settings-page-save-nonce'); ?>
</form>
</div><!-- .wrap -->
<?php
}
add_action('load-alumns-reps_page_alumns-settings', function () {
$action = 'alumns-settings-save';
$nonce = 'alumns-settings-save-nonce';
if (!alums_reps_user_can_save($action, $nonce)) {
return;
}
// DO YOUR THING
});
function alums_reps_user_can_save($action, $nonce) {
$is_nonce_set = isset($_POST[$nonce]);
$is_valid_nonce = false;
//wp_die(printf('<pre>%s</pre>', print_r($_POST, true)));
if ($is_nonce_set) {
$is_valid_nonce = wp_verify_nonce($_POST[$nonce], $action);
}
return ($is_nonce_set && $is_valid_nonce);
}

WordPress WooCommerce add new field in each row at /my-account/subscriptions/ page

I am using WordPress with WooCommerce and WooCommerce subscriptions plugins and below is my code in which I have added a custom field called (Mindesk VAR Client User - Dropdown) to show in "Edit Subscription" admin page saving to my custom field based on subscription ID.
This is how it's looking like.
And this is my working code.
<?php
// action triggered when we go to add/edit subscription page
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');
add_action('woocommerce_process_shop_order_meta', 'saveWCSubscriptionCustomFields');
function showWCSubscriptionCustomFields($subscription) {
$currentPage = get_current_screen();
// If page is "Edit Subscription" page, then only show
if ($currentPage->action == 'add')
return;
// Getting all the users
$mindeskUsers = getAllUsers();
?>
<br class="clear" />
<p class="form-field form-field-wide">
<label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
<?php
$selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);
echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
?>
</p>
<?php
}
function saveWCSubscriptionCustomFields($subscription_id) {
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
update_post_meta($subscription_id, 'mindesk_wc_subscriptions_var_client_user_id', wc_clean($_POST['mindesk_wc_subscriptions_var_client_user_id']));
}
This is working fine for me.
Now I have a custom requirement to add a button something called as Transfer in each row in http:://www.mywebsite.com/my-account/subscriptions/ page.
For example this page somewhere beside Total.
After clicking on that button, the popup should come with a form and I should be able to save a field "Transfer to VAR Client User" based on subscription ID same as I have shown you a working code above with my custom field.
I have tried to do R & D but most of the links suggesting to add custom fields and all in "My Account" page (/my-account) But I want to achieve the same in "my-account/subscriptions" page.
Can anyone guide me how can I achieve this? Any suggestion will be highly appreciated.
Thanks
You can copy the my-subscriptions.php file from the woocommerce-subscriptions/templates/myaccount and add it to your active theme woocommerce folder create folder myaccount and paste my-subscriptions.php. and then modify as per your requirement.
<?php if ( ! empty( $subscriptions ) ) : ?>
<table class="my_account_subscriptions my_account_orders woocommerce-orders-table woocommerce-MyAccount-subscriptions shop_table shop_table_responsive woocommerce-orders-table--subscriptions">
<thead>
<tr>
<th class="subscription-id order-number woocommerce-orders-table__header woocommerce-orders-table__header-order-number woocommerce-orders-table__header-subscription-id"><span class="nobr"><?php esc_html_e( 'Subscription', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-status order-status woocommerce-orders-table__header woocommerce-orders-table__header-order-status woocommerce-orders-table__header-subscription-status"><span class="nobr"><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-next-payment order-date woocommerce-orders-table__header woocommerce-orders-table__header-order-date woocommerce-orders-table__header-subscription-next-payment"><span class="nobr"><?php echo esc_html_x( 'Next payment', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Total', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-total order-total woocommerce-orders-table__header woocommerce-orders-table__header-order-total woocommerce-orders-table__header-subscription-total"><span class="nobr"><?php echo esc_html_x( 'Transfer', 'table heading', 'woocommerce-subscriptions' ); ?></span></th>
<th class="subscription-actions order-actions woocommerce-orders-table__header woocommerce-orders-table__header-order-actions woocommerce-orders-table__header-subscription-actions"> </th>
</tr>
</thead>
<tbody>
<?php /** #var WC_Subscription $subscription */ ?>
<?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?>
<tr class="order woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $subscription->get_status() ); ?>">
<td class="subscription-id order-number woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-id woocommerce-orders-table__cell-order-number" data-title="<?php esc_attr_e( 'ID', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'woocommerce-subscriptions' ), $subscription->get_order_number() ) ); ?>
<?php do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription ); ?>
</td>
<td class="subscription-status order-status woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-status woocommerce-orders-table__cell-order-status" data-title="<?php esc_attr_e( 'Status', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?>
</td>
<td class="subscription-next-payment order-date woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-next-payment woocommerce-orders-table__cell-order-date" data-title="<?php echo esc_attr_x( 'Next Payment', 'table heading', 'woocommerce-subscriptions' ); ?>">
<?php echo esc_attr( $subscription->get_date_to_display( 'next_payment' ) ); ?>
<?php if ( ! $subscription->is_manual() && $subscription->has_status( 'active' ) && $subscription->get_time( 'next_payment' ) > 0 ) : ?>
<br/><small><?php echo esc_attr( $subscription->get_payment_method_to_display( 'customer' ) ); ?></small>
<?php endif; ?>
</td>
<td class="subscription-total order-total woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-total woocommerce-orders-table__cell-order-total" data-title="<?php echo esc_attr_x( 'Total', 'Used in data attribute. Escaped', 'woocommerce-subscriptions' ); ?>">
<?php echo wp_kses_post( $subscription->get_formatted_order_total() ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'Transfer', 'Transfer a subscription', 'woocommerce-subscriptions' ); ?>
</td>
<td class="subscription-actions order-actions woocommerce-orders-table__cell woocommerce-orders-table__cell-subscription-actions woocommerce-orders-table__cell-order-actions">
<?php echo esc_html_x( 'View', 'view a subscription', 'woocommerce-subscriptions' ); ?>
<?php do_action( 'woocommerce_my_subscriptions_actions', $subscription ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if ( 1 < $max_num_pages ) : ?>
<div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
<?php if ( 1 !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
<?php if ( intval( $max_num_pages ) !== $current_page ) : ?>
<a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'subscriptions', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce-subscriptions' ); ?></a>
<?php endif; ?>
</div>
<?php endif; ?>
<?php else : ?>
<p class="no_subscriptions woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
<?php if ( 1 < $current_page ) :
printf( esc_html__( 'You have reached the end of subscriptions. Go to the %sfirst page%s.', 'woocommerce-subscriptions' ), '', '' );
else :
esc_html_e( 'You have no active subscriptions.', 'woocommerce-subscriptions' );
?>
<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
<?php esc_html_e( 'Browse products', 'woocommerce-subscriptions' ); ?>
</a>
<?php
endif; ?>
</p>
<?php endif; ?>
Tested and works

Manage custom posttype function using an additional menu in dashboard

I have created a child theme for twenty-seventeen in wordpress and created a slider function to display slider images and also a custom post type for the function.
Now I created an extra menu under appearances in dashboard as Slider settings and I need to manage the slider using that settings.
In that settings I need to have the following
o Enable slider option (check box)
o Enable slider only for logged in users (check box)
o Set a global title for slider block (text field)
I Added in back end but no condition added in front end to display slides based on this.
How could I do this?
You can use this code and change page for rendering options:
<?php
add_action('admin_menu', 'create_menu');
function create_menu() {
add_options_page(__( 'Plugin Settings', 'textdomain' ),__( 'Plugin Settings', 'textdomain' ), 'administrator', __FILE__, 'settings_page', __FILE__);
add_action( 'admin_init', 'mysettings' );
}
function mysettings() {
register_setting( 'settings-group', 'enable_slider' );
register_setting( 'settings-group', 'enable_slider_loggedin' );
register_setting( 'settings-group', 'slider_title' );
}
function settings_page() {
?>
<div class="wrap">
<h2><?php _e('PluginSettings','textdomain'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'settings-group' ); ?>
<?php do_settings_sections( 'settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e(' Enable slider','textdomain'); ?></th>
<td>
<input name="enable_slider" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Enable slider only for logged in users','textdomain'); ?></th>
<td>
<input name="enable_slider_loggedin" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider_loggedin' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Title For Slider','textdomain'); ?></th>
<td><input type="text" name="slider_title" value="<?php echo get_option('slider_title'); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Using like this:
if ( get_option ('enable_slider') == 1 ) {
// enable
} else {
//disable
}

Adding custom admin image upload metabox field in woocommerce to product category

I am working with a custom theme and would like to add a custom upload field in the woocommerce admin section. Particularly, the categories section. I want to add another field just like the thumbnail upload that stores the information.
raunak-gupta in Adding custom field to product category in WooCommerce shares a way forward which I took on and produced this. HELP???
Added uploader.js
jQuery(document).ready( function($){
var mediaUploader_woo;
$('#upload-button-woo').on('click',function(e) {
e.preventDefault();
if( mediaUploader_woo ){
mediaUploader_woo.open();
return;
}
mediaUploader_woo = wp.media.frames.file_frame = wp.media({
title: 'Choose an Image',
button: { text: 'Choose Image'},
multiple: false
});
mediaUploader_woo.on('select', function(){
attachment = mediaUploader_woo.state().get('selection').first().toJSON();
$('#meta-woo').val(attachment.url);
});
mediaUploader_woo.open();
});
});
I called the media uploader in functions.php
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'wina_classic-uadmin-js', get_template_directory_uri() . '/js/uploader.js', array('jquery','media-upload','thickbox'), '20130115', true );
wp_enqueue_script( 'wina_classic-uadmin-js');
}
add_action('admin_print_scripts', 'my_admin_scripts');
then voila..
/*-------------------------------------------------------------------
Add Custom metabox for woocommerce Category page
---------------------------------------------------------------------*/
function product_cat_add_video_field_rj() {
?>
<div class="form-field">
<label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label>
<input type="text" name="term_meta[video_link]" id="term_meta[video_link]" value="">
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</div>
<?php
}
function product_cat_edit_video_field_rj($term) {
$t_id = $term->term_id;
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[video_link]"><?php _e( 'Video Link', 'flatsome' ); ?></label></th>
<td>
<input type="text" name="term_meta[video_link]" id="meta-woo" value="<?php echo esc_attr( $term_meta['video_link'] ) ? esc_attr( $term_meta['video_link'] ) : ''; ?>" style="margin-left: 0px; margin-right: 0px; width: 50%;" />
<input type="button" class="button button-secondary" value="Upload Image" id="upload-button-woo" />
<p class="description"><?php _e( 'Enter a Video Link','flatsome' ); ?></p>
</td>
</tr>
<?php
}
// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_video_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_video_field_rj', 10, 2 );
function product_cat_video_link_save( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_video_link_save', 10, 2 );
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_video_link_save', 10, 2 );
PS: This code does not store or reproduce the saved data in the text field.
A few hours later, I came up with this code below
Add function to functions.php
//call for woocommerce custom admin image code
require get_template_directory() . '/inc/woo-meta-category.php';
/*--------------------------------------------------------------------------------------
Uploader JS
----------------------------------------------------------------------------------------*/
function my_admin_scripts() {
wp_enqueue_media();
wp_register_script( 'wina_classic-uadmin-js', get_template_directory_uri() . '/js/uploader.js',
array('jquery','media-upload','thickbox'), '20130115', true );
wp_enqueue_script( 'wina_classic-uadmin-js');
}
add_action('admin_print_scripts', 'my_admin_scripts');
Add Media uploader Javascript in js/uploader.js
jQuery(document).ready( function($){
var mediaUploader_woo;
$('#upload-button-woo').on('click',function(e) {
e.preventDefault();
if( mediaUploader_woo ){
mediaUploader_woo.open();
return;
}
mediaUploader_woo = wp.media.frames.file_frame = wp.media({
title: 'Choose an Image',
button: { text: 'Choose Image'},
multiple: false
});
mediaUploader_woo.on('select', function(){
attachment = mediaUploader_woo.state().get('selection').first().toJSON();
$('#category-meta-woo').val(attachment.url);
$('#category-header-preview').attr('src', ''+ attachment.url + '' );
});
mediaUploader_woo.open();
});
});
Finally add /inc/woo-meta-category.php file with code below
<?php
/*-------------------------------------------------------------------
Add Custom metabox for woocommerce Category page
---------------------------------------------------------------------*/
function product_cat_add_cat_head_field_rj() { ?>
<div class="form-field">
<label for="term_meta[cat_head_link]"><?php _e( 'Category Page Image', 'wina-classic' ); ?></label>
<input type="text" name="term_meta[cat_head_link]" id="term_meta[cat_head_link]" value="">
<p class="description"><?php _e( 'Upload Category Page Image','wina-classic' ); ?></p>
</div>
<?php }
function product_cat_edit_cat_head_field_rj($term) {
$t_id = $term->term_id; $term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[cat_head_link]"><?php _e( 'Category Page Image', 'wina-classic' ); ?></label></th>
<td>
<img src="<?php echo esc_attr( $term_meta['cat_head_link'] ) ? esc_attr( $term_meta['cat_head_link'] ) : ''; ?>" height="60" width="120" id="category-header-preview" />
<input type="hidden" name="term_meta[cat_head_link]" id="category-meta-woo" value="<?php echo esc_attr( $term_meta['cat_head_link'] ) ? esc_attr( $term_meta['cat_head_link'] ) : ''; ?>" style="margin-left: 0px; margin-right: 0px; width: 50%;" />
<input type="button" class="button button-secondary" value="Upload Image" id="upload-button-woo" />
<p class="description"><?php _e( 'Upload Category Page Image','wina-classic' ); ?></p>
</td>
</tr>
<?php
}
// this action use for add field in add form of taxonomy
add_action( 'product_cat_add_form_fields', 'product_cat_add_cat_head_field_rj', 10, 2 );
// this action use for add field in edit form of taxonomy
add_action( 'product_cat_edit_form_fields', 'product_cat_edit_cat_head_field_rj', 10, 2 );
function product_cat_cat_head_link_save( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
// this action use for save field value of edit form of taxonomy
add_action( 'edited_product_cat', 'product_cat_cat_head_link_save', 10, 2 );
// this action use for save field value of add form of taxonomy
add_action( 'create_product_cat', 'product_cat_cat_head_link_save', 10, 2 );
In the front end
global $post;
//for product cat archive page only
$term = get_queried_object();
$cutomPageImageOption = get_option('taxonomy_' . $term->term_id);
$cutomPageImage = $cutomPageImageOption['cat_head_link'];
if ($cutomPageImage > 1) { echo "Please add a category head image in the admin panel"; }
?>
<section id="page-header" class="page-header" style="background-image: url('<?php echo $cutomPageImage; ?>">
<div class="container">
<div class="row">
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
</div>
</div>
</section>

Wordpress User Upload Front End: Errors Illegal String off set, file is empty. Works on backend.

So having trouble getting a file uploaded via the front end and actually saving properly.
Funny thing is when I go through the back end user panel it works just fine.
Worth noting this is all on my localhost, running MAMP (apache).
I made sure to include the right files and declare the enctype for the form. I managed to get plain text fields to save just fine. I think I am messing up this part:
if ( !empty( $_POST['map_pdf'] ) )
update_user_meta( $current_user->ID, 'map_pdf', wp_handle_upload( $_POST['map_pdf'], array( 'test_form' => false ) ));
I have seen several other solutions but this is to be displayed in with the users information.
These are the errors I am running into:
Illegal string offset 'size' in /wp-admin/includes/file.php on line 272
File is empty. Please upload something more substantial.
This error could also be caused by uploads being disabled in your php.ini
or by post_max_size being defined as smaller than upload_max_filesize in php.ini.
Profile Page
<?php
/**
* Template Name: User Profile
*
* Allow users to update their profiles from Frontend.
*
*/
/* Get user info. */
global $current_user, $wp_roles;
get_currentuserinfo();
/* Load the registration file. */
require_once( ABSPATH . WPINC . '/registration.php' );
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$error = array();
/* If profile was saved, update profile. */
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
/* Update user password. */
if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
if ( $_POST['pass1'] == $_POST['pass2'] )
wp_update_user( array( 'ID' => $current_user->ID, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
else
$error[] = __('The passwords you entered do not match. Your password was not updated.', 'profile');
}
/* Update user information. */
if ( !empty( $_POST['url'] ) )
wp_update_user( array ('ID' => $current_user->ID, 'user_url' => esc_attr( $_POST['url'] )));
if ( !empty( $_POST['email'] ) ){
if (!is_email(esc_attr( $_POST['email'] )))
$error[] = __('The Email you entered is not valid. please try again.', 'profile');
elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
$error[] = __('This email is already used by another user. try a different one.', 'profile');
else{
wp_update_user( array ('ID' => $current_user->ID, 'user_email' => esc_attr( $_POST['email'] )));
}
}
$r = get_user_meta( $user->ID, 'map_pdf', true );
if ( !empty( $_POST['first-name'] ) )
update_user_meta( $current_user->ID, 'first_name', esc_attr( $_POST['first-name'] ) );
if ( !empty( $_POST['last-name'] ) )
update_user_meta($current_user->ID, 'last_name', esc_attr( $_POST['last-name'] ) );
if ( !empty( $_POST['display_name'] ) )
wp_update_user(array('ID' => $current_user->ID, 'display_name' => esc_attr( $_POST['display_name'] )));
update_user_meta($current_user->ID, 'display_name' , esc_attr( $_POST['display_name'] ));
if ( !empty( $_POST['description'] ) )
update_user_meta( $current_user->ID, 'description', esc_attr( $_POST['description'] ) );
if ( !empty( $_POST['map_pdf'] ) )
update_user_meta( $current_user->ID, 'map_pdf', wp_handle_upload( $_POST['map_pdf'], array( 'test_form' => false ) ));
/* Redirect so the page will show updated info.*/
/*I am not Author of this Code- i dont know why but it worked for me after changing below line to if ( count($error) == 0 ){ */
if ( count($error) == 0 ) {
//action hook for plugins and extra fields saving
do_action('edit_user_profile_update', $current_user->ID);
wp_redirect( get_permalink().'?updated=true' ); exit;
}
}
?>
<?php get_template_part('templates/header'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="contentarea tk-proxima-nova">
<div class="thecontent">
<h3>Update Information for "<?php echo $current_user->user_login ?>"</h3></br>
<?php if ( $_GET['updated'] == 'true' ) : ?> <div id="message" class="updated"><p>Your profile has been updated.</p></div> <?php endif; ?>
<?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
<div id="post-<?php the_ID(); ?>">
<div class="entry-content entry">
<?php the_content(); ?>
<?php if ( !is_user_logged_in() ) : ?>
<p class="warning">
<?php _e('You must be logged in to edit your profile.', 'profile'); ?>
</p><!-- .warning -->
<?php else : ?>
<?php if ( count($error) > 0 ) echo '<p class="error">' . implode("<br />", $error) . '</p>'; ?>
<form method="post" id="adduser" action="<?php the_permalink(); ?>">
<p class="form-username">
<label for="first-name"><?php _e('First Name', 'profile'); ?></label>
<input class="text-input" name="first-name" type="text" id="first-name" value="<?php the_author_meta( 'first_name', $current_user->ID ); ?>" />
</p><!-- .form-username -->
<p class="form-username">
<label for="last-name"><?php _e('Last Name', 'profile'); ?></label>
<input class="text-input" name="last-name" type="text" id="last-name" value="<?php the_author_meta( 'last_name', $current_user->ID ); ?>" />
</p><!-- .form-username -->
<p class="form-email">
<label for="email"><?php _e('E-mail *', 'profile'); ?></label>
<input class="text-input" name="email" type="text" id="email" value="<?php the_author_meta( 'user_email', $current_user->ID ); ?>" />
</p><!-- .form-email -->
<p class="form-url">
<label for="url"><?php _e('Website', 'profile'); ?></label>
<input class="text-input" name="url" type="text" id="url" value="<?php the_author_meta( 'user_url', $current_user->ID ); ?>" />
</p><!-- .form-url -->
<p class="form-password">
<label for="pass1"><?php _e('Password *', 'profile'); ?> </label>
<input class="text-input" name="pass1" type="password" id="pass1" />
</p><!-- .form-password -->
<p class="form-password">
<label for="pass2"><?php _e('Repeat Password *', 'profile'); ?></label>
<input class="text-input" name="pass2" type="password" id="pass2" />
</p><!-- .form-password -->
<p class="form-textarea">
<label for="description"><?php _e('Biographical Information', 'profile') ?></label>
<textarea name="description" id="description" rows="3" cols="50"><?php the_author_meta( 'description', $current_user->ID ); ?></textarea>
</p><!-- .form-textarea -->
<?php
//action hook for plugin and extra fields
do_action('edit_user_profile',$current_user);
?>
<p class="form-submit">
<?php echo $referer; ?>
<input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'profile'); ?>" />
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="update-user" />
</p><!-- .form-submit -->
</form><!-- #adduser -->
<?php endif; ?>
</div><!-- .entry-content -->
</div><!-- .hentry .post -->
</div><!-- .hentry .post -->
</div><!-- .hentry .post -->
<?php endwhile; ?>
<?php else: ?>
<p class="no-data">
<?php _e('Sorry, no page matched your criteria.', 'profile'); ?>
</p><!-- .no-data -->
<?php endif; ?>
<?php get_template_part('templates/footer'); ?>
And here is the functions.php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) {
$r = get_user_meta( $user->ID, 'map_pdf', true );
?>
<h3>file</h3>
<table class="form-table">
<tr>
<th scope="row">file</th>
<td><input type="file" name="map_pdf" value="" />
<?php //print_r($r);
if (!isset($r['error'])) {
$r = $r['url'];
echo $r;
} else {
$r = $r['error'];
echo $r;
}
?>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
$_POST['action'] = 'wp_handle_upload';
if( $_FILES['map_pdf']['error'] === UPLOAD_ERR_OK ) {
$r = wp_handle_upload( $_FILES['map_pdf'] );
update_user_meta( $user_id, 'map_pdf', $r );
}
}
add_action('user_edit_form_tag', 'make_form_accept_uploads');
function make_form_accept_uploads() {
echo ' enctype="multipart/form-data"';
}
Any help is much appreciated. I have been looking everywhere and I have seen others with similar issues but none that are as specific as mine by pulling the userID and having it post back to the user page.
I have a problem with this line just checking exists(1) vs current_user->ID:
elseif(email_exists(esc_attr( $_POST['email'] )) != $current_user->id )
$error[] = __('This email is already used by another user. try a different one.', 'profile');
Would you want to do this?
$current_user_id = $current_user->ID;
$post_email = $_POST['email'];
$esc_email = esc_attr($post_email);
$email_exists = email_exists($esc_email);
$user_by_email = get_user_by( 'email', $esc_email );
$email_user_id = isset($user_by_email->data->ID) ? $user_by_email->data->ID : 0;
}elseif( $email_user_id && ($email_user_id != $current_user_id) ){
$error[] = __('This email is already used by another user. try a different one.', 'profile')

Resources