I am working on the settings api of the wordpress but can't save the settings, as it returns nothing. I can't understand what am i doing wrong here. I am new to wordpress development so can't figure it out.
<?php
add_action( 'admin_init', function() {
register_setting( 'my-plugin-settings', 'map_option_1' );
});
function my_plugin_page() {
?>
<div class="wrap">
<form action="admin.php?page=creww_dashboard" method="post">
<?php
settings_fields( 'my-plugin-settings' );
do_settings_sections( 'my-plugin-settings' );
?>
<table>
<tr>
<th>Api Key</th>
<td><input type="text" placeholder="Your name" name="map_option_1" value="<?php echo esc_attr( get_option('map_option_1') ); ?>" size="50" /></td>
</tr>
<tr>
<td><?php submit_button(); ?></td>
</tr>
</table>
</form>
</div>
<?php
}
my_plugin_page();
Related
I have created with custom plugin 2 new fields at user profile.
Now I want to show them when a user login the values.
I write a function in theme but I cant figure out how to call it.
How I can do this?
In function.php
function my_extra_author_fields( $user ) {
$registered_date = get_the_author_meta( 'registered_date', $user->ID );
$expiry_date = get_the_author_meta( 'expiry_date', $user->ID );
?>
<h3>My Fields</h3>
<table class="form-table">
<tr>
<th><label for="registered_date">Registered date</label></th>
<td>
<input type="date" name="registered_date" id="registered_date" class="regular-text" value="<?php esc_attr( $registered_date ); ?>" />
<br />
</td>
</tr>
<tr>
<th><label for="expiry_date">Expiry date</label></th>
<td>
<input type="date" name="expiry_date" id="expiry_date" class="regular-text" value="<?php esc_attr($expiry_date); ?>" />
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'my_extra_author_fields' );
You can use get_user_meta() like this:
$registered_date = get_user_meta( $user->ID, 'registered_date' , true );
More information: https://developer.wordpress.org/reference/functions/get_user_meta/
I am new in PHP Code Igniter. How can I display different div based on returned data from model. If there are no data returned this div is returned
<div id='error_div' class='error'>No data here</div>
But if there is a minimum of 1 data. This div should be displayed:
<?php foreach($vendor as $result){ ?>
<div id='displayed_div'>
<table id="table_list">
<tr>
<td>ID Vendor</td>
<td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
</tr>
<tr>
<td>Vendor</td>
<td><input type="text" value="<?php echo $result['vendor']; ?>"</td>
</tr>
</table>
</div>
<?php }; ?>
My controller to fetch data
public function detailed_vendor(){
$id_klasifikasi = $this->uri->segment(3);
$data['vendor'] = $this->model_vendor->show_vendor_by_kls($id_klasifikasi);
$this->load->view('view_vendor_detail',$data);
}
This is my model:
function show_vendor_by_kls($id_klasifikasi){
$this->db->select('*');
$this->db->from('vendor');
$this->db->join('vendor_detail', 'vendor.id_vendor = vendor_detail.id_vendor');
$this->db->join('klasifikasi', 'vendor_detail.id_klasifikasi = klasifikasi.id_klasifikasi');
$this->db->where('vendor_detail.id_klasifikasi', $id_klasifikasi);
$this->db->where('vendor.st_aktif', 1);
return $this->db->get()->result_array();
}
It doesn't matter if you're brand new on any PHP Framework or not using any frameowrk either, as long as the Server Side's Code is a PHP language, then checking using empty keyword on variable is acceptable after all:
<?php
if ( !empty( $vendor ) ) {
foreach($vendor as $result){ ?>
<div id='displayed_div'>
<table id="table_list">
<tr>
<td>ID Vendor</td>
<td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
</tr>
<tr>
<td>Vendor</td>
<td><input type="text" value="<?php echo $result['vendor']; ?>"</td>
</tr>
</table>
</div>
<?php
}
}
else { ?>
<div id='error_div' class='error'>No data here</div>
<?php
} ?>
You should check your return data before render in view file.
<?php
if(!empty($vendor)) {
foreach($vendor as $result){ ?>
<div id='displayed_div'>
<table id="table_list">
<tr>
<td>ID Vendor</td>
<td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
</tr>
<tr>
<td>Vendor</td>
<td><input type="text" value="<?php echo $result['vendor']; ?>"</td>
</tr>
</table>
</div>
<?php }
} else { ?>
<div id='error_div' class='error'>No data here</div>
<php } ?>
You can check whether it's empty or not :
if(empty($vendor)){
echo "<div id='error_div' class='error'>No data here</div>";
} else {
foreach($vendor as $result){
echo "<div id='displayed_div'></div>";
}
}
Test if $vendor is empty before using foreach to iterate it
<? if( empty($vendor) ) {?>
<div id='error_div' class='error'>No data here</div>
<? } else {
foreach($vendor as $result){ ?>
<div id='displayed_div'>
<table id="table_list">
<tr>
<td>ID Vendor</td>
<td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
</tr>
<tr>
<td>Vendor</td>
<td><input type="text" value="<?php echo $result['vendor']; ?>" </td>
</tr>
</table>
</div>
<? }} ?>
<?php function my_plugin_settings_page() {?>
<div class="wrap">
<h2>Staff Details</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Accountant Name</th>
<td><input type="text" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Accountant Phone Number</th>
<td><input type="text" name="accountant_phone" value="<?php echo esc_attr( get_option('accountant_phone') ); ?>" /></td>
</tr>
</table>
<?php submit_button( 'Save Settings', 'primary', 'wpdocs-save-settings' );
if (isset($_GET['submit'])) {
header('Location: wamp\www\wordpress\wordpress\wp-content\plugins\MaximoPlugin\andra.php');
exit;
}?>
</form>
</div>
My question is, can i redirect a user to another php file in wordpress by adding action to the button_submit ?
So it has like two events. Saving and then redirect the user to another page.
Is this possible on a way. BTW im new to php and wordpress so im sorry for the bad code and stuff.
the if statement isent any good i know, but how can i check if the buttons is submitted and then redirect the user to another wordpress page..(another php site.)
Please give me some input for what i can do or should do.
EDIT:
Ok i changed it to a include, but dont get any response.
</table>
<?php submit_button();
if(isset($_POST['submit']))
{
include('andra.php');
}
}?>
You can use wp_redirect http://codex.wordpress.org/Function_Reference/wp_redirect to redirect the user to another URL. Simply save your data before redirect call.
<?php
wp_redirect( $location, $status );
exit;
?>
The questions is not very clear so I will show two solutions, you can pick the one suits you more:
SOLUTION 1:
Make another file handle_my_request.php
Change the following lines in your code:
<?php submit_button( 'Save Settings', 'primary', 'wpdocs-save-settings' );
if (isset($_GET['submit'])) {
include_once("handle_my_request.php");
exit;
}?>
Now in handle_my_request.php, you can easily write the way you want to handle the request. Eg.
<?php
$var1 = $_GET['myvar1'];
do_something($var1);
?>
SOLUTION 2:
You manually need to make another form and post the fields to the other page you want. For this to happen, you can either GET/POST on server side through PHP (using CURL) or you can GET/POST through javascript on client side.
<?php function my_plugin_settings_page() {?>
<div class="wrap">
<h2>Staff Details</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Accountant Name</th>
<td><input type="text" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Accountant Phone Number</th>
<td><input type="text" name="accountant_phone" value="<?php echo esc_attr( get_option('accountant_phone') ); ?>" /></td>
</tr>
</table>
</form>
<?php
submit_button( 'Save Settings', 'primary', 'wpdocs-save-settings' );
if (isset($_GET['submit']))
{ ?>
<form name="redirection_form" action="http://anotherwebsite.com" method="post">
<input type="hidden" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" />
<input type="hidden" name="accountant_phone" value="<?php echo esc_attr( get_option('accountant_phone') ); ?>" />
</form>
<script type="text/javascript">
document.redirection_form.submit();
</script>
<?php } ?>
</div>
Needless to say here that you need to change the variables and method and action of the form to match the next page you are redirecting to.
As I said before, the same can be achieved by PHP CURL.
<form method="post" action="options.php">
<input type='hidden' name='option_page' value='my-plugin-settings-group' /><input type="hidden" name="action" value="update" /><input type="hidden" id="_wpnonce" name="_wpnonce" value="fc1447e3e7" /><input type="hidden" name="_wp_http_referer" value="/wordpress/wordpress/wp-admin/admin.php?page=my-plugin-settings&settings-updated=true" /> <table class="form-table">
<tr valign="top">
<th scope="row">Accountant Name</th>
<td><input type="text" name="accountant_name" value="1" /></td>
</tr>
<tr valign="top">
<th scope="row">Accountant Phone Number</th>
<td><input type="text" name="accountant_phone" value="1" /></td>
</tr>
</table>
<p class="submit"><input type="submit" name="wpdocs-save-settings" id="wpdocs-save-settings" class="button button-primary" value="Save Settings" /></p>
This is the source view. And yes the andra.php is in same directory.
Im making a plugin for Wordpress, do it have something to do with that ?
I have been trying to add a multi select array from another plugin into the functions.php of a wordpress theme or a separate plugin. the field is "US_States_Serviced" i can get the code to know the the wp_usermeta is there, but returns ARRAY in the text field. I am assuming i need to ad the ARRAY. which i have no clue how to do. I looked, and searched but no codes really look uniform to me and match what i want as far as an array. Cant the plugin, or functions.php pull the array automatically if i set the TYPE= correctly? Is multi select not native to Wordpress. Just lost
the "US_States_Serviced" field located in the wp_usermeta has all US STATES that allows a user to select however many he services. Now i need to reflect this data into the user profile of wordpress. I been trying 2 approaches. Which is best, and can some one help me please.
OPTION 1 ADD TO 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 ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="US_States_Serviced"><?php _e("US State You Service"); ?></label></th>
<td>
<input type="text" name="US_States_Serviced" id="US_States_Serviced" value="<?php echo esc_attr( get_the_author_meta( 'US_States_Serviced', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter all states serviced."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("City"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your city."); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e("Province"); ?></label></th>
<td>
<input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your province."); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your postal code."); ?></span>
</td>
</tr>
</table>
OPTION 2 AS A PLUGIN
class shw_user_meta {
function shw_user_meta() {
if ( is_admin() )
{
add_action('show_user_profile', array(&$this,'action_show_user_profile'));
add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
add_action('personal_options_update', array(&$this,'action_process_option_update'));
add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
}
}
function action_show_user_profile($user)
{
?>
<h3><?php _e('EXTRA PROFILE INFORMATION') ?></h3>
<table>
<tr>
<th><label for="member_id"><?php _e('Member ID'); ?></label></th>
<td><input type="text" name="member_id" id="Memmber ID" value="<?php echo esc_attr(get_the_author_meta('member_id', $user->ID) ); ?>" /></td>
<th><label for="us_states"><?php _e('US States you Service'); ?></label></th>
<td><input type="multi" name="US_States_Serviced" id="US States you Service" value="<?php echo esc_attr(get_the_author_meta('US_States_Serviced', $user->ID) ); ?>" /></td>
<th><label for="user-registered"><?php _e('User Registered'); ?></label></th>
<td><input type="text" name="user_registered" id="user_registered" value="<?php echo esc_attr(get_the_author_meta('user_registered', $user->ID) ); ?>" /></td>
</tr>
</table>
<?php
}
function action_process_option_update($user_id)
{
if(isset($_POST['member_id'])){
update_user_meta( $user_id, 'member_id', $_POST['member_id'] );
}
if(isset($_POST['us_states'])){
update_user_meta( $user_id, 'us_states', $_POST['us_states'] );
}
if(isset($_POST['user_registered'])){
update_user_meta( $user_id, 'user_registered', $_POST['user_registered'] );
}
}
}
/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $shw_user_meta_instance; $shw_user_meta_instance = new shw_user_meta();'));
?>
Im having an issues when activating my theme in wordpress. I got this error message:
Parse error: syntax error, unexpected
$end in
C:\xampp\htdocs\xampp\wordpress\wp-content\themes\xit\functions.php
on line 223
Whats wrong, I totally dont understand. The script of the php is:
<?php if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
function content($num) {
$theContent = get_the_content();
$output = preg_replace('/<img[^>]+./','', $theContent);
$limit = $num+1;
$content = explode(' ', $output, $limit);
array_pop($content);
$content = implode(" ",$content)."...";
echo $content;
}
function post_is_in_descendant_category( $cats, $_post = null )
{
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
//custom comments
function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard">
<div class="comment-meta commentmetadata">
<?php echo get_avatar($comment,$size='32',$default='http://www.gravatar.com/avatar/61a58ec1c1fba116f8424035089b7c71?s=32&d=&r=G' ); ?>
<?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?> <br /><?php printf(__('<strong>%s</strong> says:'), get_comment_author_link()) ?><?php edit_comment_link(__('(Edit)'),' ','') ?></div>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<br />
<?php endif; ?>
<div class="text"><?php comment_text() ?></div>
<div class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>
</div>
<?php }
add_action('admin_menu', 'xit_theme_page');
function xit_theme_page ()
{
if ( count($_POST) > 0 && isset($_POST['xit_settings']) )
{
$options = array ( 'style','logo_img','logo_alt','logo_txt', 'logo_tagline', 'tagline_width', 'contact_email','ads', 'advertise_page', 'twitter_link', 'facebook_link', 'flickr', 'about_tit', 'about_txt', 'analytics');
foreach ( $options as $opt )
{
delete_option ( 'xit_'.$opt, $_POST[$opt] );
add_option ( 'xit_'.$opt, $_POST[$opt] );
}
}
add_theme_page(__('Xit Options'), __('Xit Options'), 'edit_themes', basename(__FILE__), 'xit_settings');
}
function xit_settings ()
{?>
<div class="wrap">
<h2>XIT Options Panel</h2>
<form method="post" action="">
<table class="form-table">
<!-- General settings -->
<tr>
<th colspan="2"><strong>General Settings</strong></th>
</tr>
<tr valign="top">
<th scope="row"><label for="style">Theme Color Scheme</label></th>
<td>
<select name="style" id="style">
<option value="pink.css" <?php if(get_option('xit_style') == 'pink.css'){?>selected="selected"<?php }?>>pink.css</option>
<option value="blue.css" <?php if(get_option('xit_style') == 'blue.css'){?>selected="selected"<?php }?>>blue.css</option>
<option value="orange.css" <?php if(get_option('xit_style') == 'orange.css'){?>selected="selected"<?php }?>>orange.css</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="logo_img">Logo image (full path to image)</label></th>
<td>
<input name="logo_img" type="text" id="logo_img" value="<?php echo get_option('xit_logo_img'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="logo_alt">Logo image ALT text</label></th>
<td>
<input name="logo_alt" type="text" id="logo_alt" value="<?php echo get_option('xit_logo_alt'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="logo_txt">Text logo</label></th>
<td>
<input name="logo_txt" type="text" id="logo_txt" value="<?php echo get_option('xit_logo_txt'); ?>" class="regular-text" />
<br /><em>Leave this empty if you entered an image as logo</em>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="logo_tagline">Logo Tag Line</label></th>
<td>
<input name="logo_tagline" type="text" id="logo_tagline" value="<?php echo get_option('xit_logo_tagline'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="tagline_width">Tag Line Box Width (px)</label><br /><em style="font-size:11px">Default width: 300px</em></th>
<td>
<input name="tagline_width" type="text" id="tagline_width" value="<?php echo get_option('xit_tagline_width'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="contact_email">Email Address for Contact Form</label></th>
<td>
<input name="contact_email" type="text" id="contact_email" value="<?php echo get_option('xit_contact_email'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="twitter_link">Twitter link</label></th>
<td>
<input name="twitter_link" type="text" id="twitter_link" value="<?php echo get_option('xit_twitter_link'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="facebook_link">Facebook link</label></th>
<td>
<input name="facebook_link" type="text" id="facebook_link" value="<?php echo get_option('xit_facebook_link'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="flickr">Flickr Photostream</label></th>
<td>
<select name="flickr" id="flickr">
<option value="yes" <?php if(get_option('xit_flickr') == 'yes'){?>selected="selected"<?php }?>>Yes</option>
<option value="no" <?php if(get_option('xit_flickr') == 'no'){?>selected="selected"<?php }?>>No</option>
</select>
<br /><em>Make sure you have FlickrRSS plugin activated if you choose to enable Flickr Photostream</em>
</td>
</tr>
<!-- Sidebar ABout Box-->
<tr>
<th colspan="2"><strong>Sidebar About Box</strong></th>
</tr>
<tr valign="top">
<th scope="row"><label for="about_tit">Title</label></th>
<td>
<input name="about_tit" type="text" id="about_tit" value="<?php echo get_option('xit_about_tit'); ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="about_txt">Text</label></th>
<td>
<textarea cols="60" rows="5" name="about_txt" type="text" id="about_txt" class="regular-text" /><?php echo get_option('xit_about_txt'); ?></textarea>
</td>
</tr>
<!-- Ads Box Settings -->
<tr>
<th colspan="2"><strong>Ads Box Settings</strong></th>
</tr>
<tr>
<th><label for="ads">Ads Section Enabled:</label></th>
<td>
<select name="ads" id="ads">
<option value="yes" <?php if(get_option('xit_ads') == 'yes'){?>selected="selected"<?php }?>>Yes</option>
<option value="no" <?php if(get_option('xit_ads') == 'no'){?>selected="selected"<?php }?>>No</option>
</select>
<br /><em>Make sure you have AdMinister plugin activated and have the position "Sidebar" created within the plugin.</em>
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="advertise_page">Advertise Page</label></th>
<td>
<?php wp_dropdown_pages("name=advertise_page&show_option_none=".__('- Select -')."&selected=" .get_option('xit_advertise_page')); ?>
</td>
</tr>
<!-- Google Analytics -->
<tr>
<th><label for="ads">Google Analytics code:</label></th>
<td>
<textarea name="analytics" id="analytics" rows="7" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('xit_analytics')); ?></textarea>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
<input type="hidden" name="xit_settings" value="save" style="display:none;" />
</p>
</form>
</div>
<? }?>
<?php function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
} ?>
The last line is line 223.
You have an extra ?> on this line $GLOBALS['comment'] = $comment; ?>.
This causes PHP to stop reading the following to read as PHP code. The number of opening <?php doesn't match the number of closing ?>
You probably have a spurious character or unnasigned piece coding - like a stray
Got it! Im so stupid! Goshhh!! The culprit was: <? }?>. It should have been: <?php } ?> Thanks for the lession guys. You guys gave me some vital clues. Loving this site!!!