Validate custom fields in wordpress post - php

I have a custom post type speaker and in speaker I have a custom field speaker_organization. How can I validate this and display error in admin notice.
add_action( 'add_meta_boxes', 'speaker_organization_box' );
function speaker_organization_box() {
add_meta_box(
'speaker_organization',
__( 'Organization', 'dbem' ),
'speaker_organization_box_content',
'speaker',
'side',
'high'
);
}
function speaker_organization_box_content( $post ) {
// generate a nonce field
wp_nonce_field( basename( __FILE__ ), 'dbem-speaker-organization-nonce' );
// get previously saved meta values (if any)
$speaker_organization = get_post_meta( $post->ID, 'speaker_organization', true );
echo '<label for="speaker_organization"></label>';
echo '<input type="text" id="speaker_organization" name="speaker_organization" placeholder="Organization Name" value="'.$speaker_organization.'" />';
}
function speaker_organization_box_save( $post_id ) {
$speaker_organization = $_POST['speaker_organization'];
update_post_meta( $post_id, 'speaker_organization', $speaker_organization );
}
add_action( 'save_post', 'speaker_organization_box_save' );

using
add_action( 'admin_notices', 'my_admin_notice' );
or using validate js
add_action('admin_enqueue_scripts', 'add_my_js');
function add_my_js(){
wp_enqueue_script('my_validate', 'path/to/jquery.validate.min.js', array('jquery'));
wp_enqueue_script('my_script_js', 'path/to/my_script.js');
}
jQuery().ready(function() {
jQuery("#post").validate();
});
<input type="text" name="my_custom_text_field" class="required"/>
function wpse_update_post_custom_values($post_id, $post) {
// Do some checking...
if($_POST['subhead'] != 'value i expect') {
// Add an error here
$errors->add('oops', 'There was an error.');
}
return $errors;
}
add_action('save_post','wpse_update_post_custom_values',1,2);
validate js code
using condition + admin notice

Related

Adding custom field through functions.php

I'm trying to add a custom field just below the price in my Woocommerce theme. I was able to add simple html by using:
add_action( 'woocommerce_before_add_to_cart_form', 'SomeName' );
function SomeName() {
echo '<p>Some Text Here</p>';
}
But what I want to do is add a custom field key instead. I'm using this code for placing custom fields:
<?php
$naslov = get_post_meta($post->ID, 'Naslov', true);
if ($naslov) { ?>
<h2 class="single-naslov-kat"><? echo $naslov; ?></h2>
<?php
} else {
// do nothing;
}
?>
The code works great when I add it to content-single-product.php theme file. But I can't add it below the price through that file. And I have no idea how to incorporate it through functions.php.
If you have any other suggestions on how I might be able to add custom text below the price for each specific product that's ok too.
Any help would be greatly appreciated.
In your themes functions.php add the code,
//create custom field
function cfwc_create_custom_field() {
$args = array(
'id' => 'custom_field',
'label' => __( 'Custom Field', 'cfwc' ),
'class' => 'cfwc-custom-field',
'desc_tip' => true,
'description' => __( 'Enter Custom Field Description.', 'ctwc' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_general_product_data', 'cfwc_create_custom_field' );
// save custom field
function cfwc_save_custom_field( $post_id ) {
$link = wc_get_product( $post_id );
$title = isset( $_POST['custom_field'] ) ? $_POST['custom_field'] : '';
$link->update_meta_data( 'custom_field', sanitize_text_field( $title ) );
$link->save();
}
add_action( 'woocommerce_process_product_meta', 'cfwc_save_custom_field' );
// display custom field in single.php page
add_action('woocommerce_before_add_to_cart_form','cmk_additional_button');
function cmk_additional_button() {
global $product;
$custom_field = $product->get_meta('custom_field');
if(!empty($custom_field)) {
echo "<a href='$custom_field' target='_blank'><button type='button' class='button alt'>Custom Field</button></a>";
}
}

Wordpress admin-ajax.php error 400 bad request

hello im doing save for later feature in my website
but i get admin-ajax bad request when i click the button
functions.php
function zumra_scripts() {
wp_register_script( 'remove-prodduct-from-list', get_template_directory_uri() . '/js/remove-product.js', array('jquery'), false, true );
wp_localize_script( 'remove-prodduct-from-list', 'sfl_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )) );
if ( is_page('savelist') ) {
wp_enqueue_script( 'remove-prodduct-from-list' );
}
}
add_action( 'wp_enqueue_scripts', 'zumra_scripts' );
save-for-later.php
<?php
add_action( 'wp_ajax_my_action', 'my_action' );
function my_action() {
$user = $_POST['user'];
$post_id = $_POST['post_id'];
$response = $_POST['saveForLater'];
$response .= ''. __( 'Browse Savelist', 'zumra' ) .'';
add_user_meta( $user, 'product_id', $post_id);
echo $response;
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_remove_product_from_list', 'remove_product_from_list' );
function remove_product_from_list() {
$user = intval( $_POST['user'] );
$product = intval( $_POST['product'] );
delete_user_meta( $user, 'product_id', $product);
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_move_to_cart', 'move_to_cart' );
function move_to_cart() {
$user = intval( $_POST['user'] );
$product = intval( $_POST['product'] );
delete_user_meta( $user, 'product_id', $product);
// do_action( 'woocommerce_ajax_added_to_cart', $product );
// wc_add_to_cart_message( $product );
wp_die(); // this is required to terminate immediately and return a proper response
}
save-for-later.js
jQuery(document).ready(function($) {
$('.ajax-form').on('submit',function(e){
e.preventDefault();
var data = {
'action': 'my_action',
'saveForLater': sfl_ajax.response,
'user': sfl_ajax.user,
'post_id': sfl_ajax.post_id,
'product_id': sfl_ajax.user_product,
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(sfl_ajax.ajaxurl, data, function(response) {
$('.save-for-later').html(response);
});
});
});
i don't know what im doing wrong
i get error admin-ajax.php 400 every time i click add to save for later button
Try with below code. I am assuming that you have include this file in the functions.php of your active theme.
Or
You can add this code in your functionctions.php directly.
As is described in the Wordpress AJAX documentation, you have two different hooks - wp_ajax_(action), and wp_ajax_nopriv_(action). The difference between these is:
wp_ajax_(action): This is fired if the ajax call is made from inside
the admin panel.
wp_ajax_nopriv_(action): This is fired if the ajax
call is made from the front end of the website.
add_action( 'wp_ajax_my_action', 'my_action' );
add_action("wp_ajax_nopriv_my_action","remove_product_from_list");
function my_action() {
$user = $_POST['user'];
$post_id = $_POST['post_id'];
$response = $_POST['saveForLater'];
$response .= ''. __( 'Browse Savelist', 'zumra' ) .'';
add_user_meta( $user, 'product_id', $post_id);
echo $response;
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_remove_product_from_list', 'remove_product_from_list' );
add_action("wp_ajax_nopriv_remove_product_from_list","remove_product_from_list");
function remove_product_from_list() {
$user = intval( $_POST['user'] );
$product = intval( $_POST['product'] );
delete_user_meta( $user, 'product_id', $product);
wp_die(); // this is required to terminate immediately and return a proper response
}
add_action( 'wp_ajax_move_to_cart', 'move_to_cart' );
add_action("wp_ajax_nopriv_move_to_cart","GetEditForm");
function move_to_cart() {
$user = intval( $_POST['user'] );
$product = intval( $_POST['product'] );
delete_user_meta( $user, 'product_id', $product);
// do_action( 'woocommerce_ajax_added_to_cart', $product );
// wc_add_to_cart_message( $product );
wp_die(); // this is required to terminate immediately and return a proper response
}

on creating custom option page in wordpress

on creating custom field in option page for my wordpress theme it show some error on each input type text call_user_func() expects parameter 1 to be a valid callback,class 'MySettingsPage' does not have a method logo_callback() or social_callback() please suggest some solution
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$this->options = get_option( 'bguru_logo' );
$this->options = get_option( 'bguru_vimeo' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback()' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback()' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf(`enter code here`
'<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf(
'<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
After Look Into Your Code, I think You need to remove () From array($this,'logo_callback()' ).
See below code, i have change & fixed:-
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$this->options['bguru_logo'] = get_option( 'bguru_logo' );
$this->options['bguru_vimeo'] = get_option( 'bguru_vimeo' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf('<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf('<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Hope this will work for you.

WordPress option page not showing submitting value

I have to create an option page using array in my wordpress theme. after submitting all fill up information removed while i am using
var_dump( $this->options) show blank array(){}
I want when submitting option page form submit information show in the input box and i will get_option value in some variable so that i have to display the information in my theme.
<?php
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$bguru_logo = is_array( get_option( 'bguru_logo' ) ) ? get_option( 'bguru_logo' ) : array();
$bguru_vimeo = is_array( get_option( 'bguru_vimeo' ) ) ? get_option( 'bguru_vimeo' ) : array();
$this->options = array_merge( $bguru_logo, $bguru_vimeo);
var_dump($this->options);
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf('<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf('<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();

PHP - If information exists in an array already, display that information instead

I have a Wordpress plugin settings page which uses the following plugin settings page:
<?php
class NewPlugin
{
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'New Plugin',
'manage_options',
'new-plugin-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'new_plugin_options' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>New Plugin Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'new_plugin_options_group' );
do_settings_sections( 'new-plugin-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'new_plugin_options_group', // Option group
'new_plugin_options', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Settings', // Title
array( $this, 'print_section_info' ), // Callback
'new-plugin-admin' // Page
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslineone',
'Address Line One',
array( $this, 'addresslineone_callback' ),
'new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslinetwo',
'Address Line Two',
array( $this, 'addresslinetwo_callback' ),
'new-plugin-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
if( isset( $input['addresslineone'] ) )
$new_input['addresslineone'] = sanitize_text_field( $input['addresslineone'] );
if( isset( $input['addresslinetwo'] ) )
$new_input['addresslinetwo'] = sanitize_text_field( $input['addresslinetwo'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="new_plugin_options[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function addresslineone_callback()
{
printf(
'<input type="text" id="addresslineone" name="new_plugin_options[addresslineone]" value="%s" />',
isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
);
}
public function addresslinetwo_callback()
{
printf(
'<input type="text" id="addresslinetwo" name="new_plugin_options[addresslinetwo]" value="%s" />',
isset( $this->options['addresslinetwo'] ) ? esc_attr( $this->options['addresslinetwo']) : ''
);
}
}
if( is_admin() )
$new_plugin_settings = new NewPlugin();
I already had a widget where the details asked for are in an array "$instance". I am trying to write an if statement which asks if $instance exists, then convert the strings to the new array.. So far I have not had much luck. Can anyone point me in the right direction?
As there is not much information about the $instance variable, these functions should help depending on the condition to consider that a variable exists :
isset
empty
property_exists

Categories