How to have multiple sections in the same theme options page - wordpress

For my theme, I have created a Settings Page which is shown in the Appearance menu of Wordpress admin.
Now, I have been following this tutorial.
Exactly like the tutorial, I have created multiple sections and I face the issue where only the fields of the last section are saved in the DB while the fields of other sections are not saved.
I do not want to go for the Tabbed Navigation option - I have too many sections.
How do I solve the problem of not being able to save fields of other sections due to the nonce issue?
EDIT: I am using Wordpress 3.3.1

Try to separate section forms...
Just like this:--
<?php if( $active_tab == 'display_options' ) { ?>
<form method="post" action="options.php">
<?php
settings_fields( 'sandbox_theme_display_options' );
do_settings_sections( 'sandbox_theme_display_options' );
submit_button();
?>
</form>
<?php } else { ?>
<form method="post" action="options.php">
<?php
settings_fields( 'sandbox_theme_social_options' );
do_settings_sections( 'sandbox_theme_social_options' );
submit_button();
?>
</form>
<?php } ?>
May be this will help you...
In tutorial document, Form is like following...
<form method="post" action="options.php">
<?php
if( $active_tab == 'display_options' ) {
settings_fields( 'sandbox_theme_display_options' );
do_settings_sections( 'sandbox_theme_display_options' );
} else {
settings_fields( 'sandbox_theme_social_options' );
do_settings_sections( 'sandbox_theme_social_options' );
} // end if/else
submit_button();
?>
</form>
This is also separate form content.

Related

Issues with Editor role and custom menu page in WordPress

I have a custom menu item in my WordPress admin section to allow for the setting of a number.
This menu it is visible by my editors, but when they attempt to update the field, they are greeted with a message that says they are not allowed to manage these options.
Here is a rough breakdown of the script:
<?php
if(is_admin()){
add_action( 'admin_menu', 'progress_menu' );
}
function progress_menu() {
add_menu_page( 'Progress Bar', 'Progress Bar', 'edit_pages', __FILE__, 'progress_page', 'dashicons-admin-generic' );
add_action( 'admin_init', 'progress_register' );
}
function progress_register() {
register_setting( 'my-site', 'progress' );
}
function progress_page() {
?>
<div class="wrap">
<h2>Progress Bar</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-site' ); ?>
<?php do_settings_sections( 'my-site' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Progress</th>
<td><input type="number" min="0" max="100" name="progress" value="<?php echo esc_attr( get_option('progress') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Any thoughts why my users with the editor role are unable to update this?
As documented:
If you are using the Settings API to save data, and need the user to
be other than the administrator, you need to modify the permissions
via the hook
option_page_capability_{$option_group},
where $option_group is the same as $option_group in
register_setting().
So basically, just use the example on that page and simply change the option_page_capability_my-options-group to option_page_capability_my-site. I.e.
// Modify capability - allow non admins to save the options.
function wpdocs_my_page_capability( $capability ) {
return 'edit_others_posts'; // or edit_pages if you want
}
// you used register_setting( 'my-site', 'progress' ), so use my-site in the 1st parameter
add_filter( 'option_page_capability_my-site', 'wpdocs_my_page_capability' );
Happy coding!

How to save in wp_options table as WordPress standards?

I have a form it sends data to options.php. After that that saved value takes and imported into textarea.
<form method="post" action="options.php">
<?php
settings_fields( 'my_option_group' );
do_settings_sections( 'my_settings_admin' );
submit_button();
?>
</form>
This code works, but if I want to save in DB some default value manually, the value doesn't come into textarea's value. I'm using add_option or update_option.
Understand this flow to save the value to database,
/* code for adding menu and options in admin */
add_action('admin_menu','admin_menu_adding');
add_action('admin_init','admin_reg_settings');
function admin_menu_adding(){
add_menu_page('Mega Page','Mega Page','administrator','mega-page','settings_page_test');
}
function settings_page_test(){
echo "test";
?>
<form method="post" action="<?php echo admin_url('admin.php'); ?>">
<?php
settings_fields('local-settings');
do_settings_sections('local-settings');
?>
<?php submit_button(); ?>
<input type="hidden" name="action" value="trotbgo" />
</form>
<?php
}
add_action('admin_action_trotbgo','save_value_here');
function save_value_here(){
update_option() //update option goes here
exit;
}
function admin_reg_settings(){
register_setting('local-settings','fieldone');
register_setting('local-settings','fieldtwo');
}
You need to save via the add_option/update_option inside save_value_here() function

Two Place Order Button in woocommerce Checkout page

I am having Issue in my checkout page. I am getting Two Place order button(click here). I tried to fix it in form-checkout.php even I checked in form-pay.php, in wp-content/themes/mytheme/woocommerce/checkout.
I checked in wordpress forum I got solution to remove two checkout button from cart page, there is no solution for my problem
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
wc_print_notices();
do_action( 'woocommerce_before_checkout_form', $checkout );
// filter hook for include new pages inside the payment method
$get_checkout_url = apply_filters( 'woocommerce_get_checkout_url', WC()->cart->get_checkout_url() ); ?>
<form name="checkout" method="post" class="checkout" action="<?php echo esc_url( $get_checkout_url ); ?>">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<div class="row" id="customer_details">
<div class="col-md-6">
<?php do_action( 'woocommerce_checkout_billing' ); ?>
</div>
<div class="col-md-6">
<?php do_action( 'woocommerce_checkout_shipping' ); ?>
</div>
</div>
<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php endif; ?>
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</form>
<?php do_action( 'woocommerce_after_checkout_form' ); ?>
Go to wp-content/themes/mytheme/woocommerce/checkout/view-order.php. In View-order.php remove id="payment" completle
This issue is because of theme version
Please check your configuration in your WordPress admin.
Woocommers->settings on the Payment Tab check your information details if you have miss an HTML tags.
I have experience on this when I customized or add some html tags on the details text area with closing "div" tag.

Changing the Purchase Button - Woocommerce

I have to hide the possibility to buy for not logged in users (whole domain or store)
Ex:
If a user is NOT logged in (don't have an account) then the cart button etc will not show. Sees only a link "See store locator list".
Only if user logged in will they show prices and button "Buy" and they will have the opportunity to purchase.
example code
if(is_user_logged_in()
Buy
if(is_user_NOT_logged_in()
Show "See store locator list" with page URL.
How to do it?
Please could someone help?
I found this example: Function Reference/is user logged in
Display different output depending on whether the user is logged in or not.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
Did it change in the "/WooCommerce/Templates/Loop/add-to-cart.php" enough?
But still need help :(
How to put this code into "buy" button?
Your code isn't exactly set up for your needs. It would be more like
<?php if (is_user_logged_in()) {?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
) );
?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php } else { ?>
<div>Store Location</div>
<?php } ?>
Inside the div/form (doesn't need to be a div, but I just put there as an example) you can put whatever code you need.
I put in the form code for displaying the simple button. You would likely need this in all your add-to-cart templates. I'm also not entirely positive what is in the before/after add to cart button so those locations might need to be tweaked.

Wordpress: show comment on a new page?

I need to display link after each comment, when you click that link, a new page displays that single coment on a new page.
Is that possible?
I answered your exact question over on WordPress Answers (also a StackExchange site) just yesterday. You can find that answer here. It involved the following four steps:
Setting up the URL Rewriting by adding a query_var, rewrite_tag and a permastruct,
Being sure to flush the rewrite rules in a plugin's activation hook or manually,
Adding a parse_query filter hook to set the query_vars's post to be the comment's post and to disable sticky posts for the query,
Adding a template_include filter hook to filter the template file name to load a template specific template file for a single comment, and lastly
To create the comment template file as /wp-content/themes/%your-theme%/comment.php.
Again, you can find the answer over here.
Hope this helps.
-Mike
UPDATE:
Below is the full content that I had also posted on WordPress Answers:
There are numerous different ways to accomplish this, some more polished than others and practically all of them with potential for conflict with other plugins, but ignoring all that here's one way that is pretty close to what you asked for. :)
This solution will support a URL format like the following where %comment_id% is the numeric ID of your comment from the wp_comments table:
http://example.com/comments/%comment_id%/
First you'll need to configure your URL rewriting using the following code. Hopefully it is reasonably self-explanitory but don't hesitate to ask:
$wp->add_query_var('comment_id'); // Add the "behind-the-scenes" query variable that WordPress will use
$wp_rewrite->add_rewrite_tag('%comment_id%', '([0-9]+)','comment_id='); // Define a rewrite tag to match that assigns to the query var
$wp_rewrite->add_permastruct('comment-page', 'comments/%comment_id%'); // Define a URL pattern to match the rewrite tag.
You'll also either need to call this code in a plugin activation hook to flush the rules, or if it's your site you can just save permalinks in the admin console's Settings > Permalinks settings area:
global $wp_rewrite;
$wp_rewrite->flush_rules(false);
Next add a parse_query filter hook. This will be called after WordPress has inspected the query. It tests to see if your added comment_id query_var set and if so it tests to see if you are on the desired URL. If yes then it loads the comment array using get_comment() in order to set the 'p' parameter (which should be set to a post ID) to the post that is related to the comment. That way when WordPress runs the query that it is going to run no matter what at least it loads something you'll need in your comment.php theme template file below and you won't have to ran another query later when you need it. This code also tells WordPress to ignore sticky posts using the oddly named caller_get_posts option:
add_filter( 'parse_query', 'my_parse_query' );
function my_parse_query( $query ) {
global $wp;
if (isset($query->query['comment_id']) && substr($wp->request,0,9)=='comments/') {
$comment = get_comment($query->query['comment_id']);
$query->query_vars['p'] = $comment->comment_post_ID; // Causes the comment's post to be loaded by the query.
$query->query_vars['caller_get_posts'] = true; // Keeps sticky posts from invading into the top of our query.
}
}
Still next you'll need to hook the code in /wp-includes/template-loader.php using the template_include filter. This will be called after WordPress has both inspected the query and loaded the post for the comment. Here you'll first check again for comment_id in the query_var and also for the URL being the one you want. If so we replace the /index.php template page with /comment.php which is a theme template file you will need to create:
add_filter( 'template_include', 'my_template_include' );
function my_template_include( $template ) {
global $wp,$wp_query;
if (isset($wp_query->query['comment_id']) && substr($wp->request,0,9)=='comments/') {
$template = str_replace('/index.php','/comment.php',$template);
}
return $template;
}
Lastly now you need to create your theme template file which I've chosen to call /comment.php. Since it's your theme you'll want to make it look like you want but here is an example to get you started:
<?php
/*
* File: /wp-content/themes/my-theme/comment.php
*/
global $wp_query,$post;
$comment_id = $wp_query->query['comment_id'];
$comment = get_comment($comment_id);
$permalink = get_permalink($post->ID);
get_header();
?>
<div id="container">
<div id="comment-<?php echo $comment_id; ?>" class="comment">
<p>Comment by: <span class="comment-author">
<?php echo $comment->comment_author; ?></span>
on <span class="comment-date"><?php echo date("D M jS Y", strtotime($comment->comment_date)); ?></span>
at <span class="comment-time"><?php echo date("h:ia", strtotime($comment->comment_date)); ?></span>
</p>
<p>About: <?php echo $post->post_title; ?></p>
<blockquote><?php echo $comment->comment_content; ?></blockquote>
</div>
</div>
<?php
get_sidebar();
get_footer();
Any questions? Just ask.
Hope this helps.
P.S. All of the code I describing above can either go in your theme's functions.php file and/or in a plugin of your own. A caveat is for the URL rewrite flushing rules that should go in a plugin activation hook if you are going to include it instead us just flushing them manually in the permalinks section of the admin console. I didn't show how to register an activation hook do but if you want to learn more you can read about it here.
(New edited version after OP's comments)
There are many ways to do this. In theory this is the simplest, but maybe not 'most appropriate according to WordPress' way. Take this as a starting point. I haven't tested it, so you may encounter an error or two that should be solvable with some minor tweaks. Let me know if you get stumped and I'll do my best. So conceptually...
You should copy the page.php template file and rename it to 'comments_page.php' (or whatever you like). Open this file in your code editor and find where the following appears: (or create it if it does not exist)
/*Template Name: page*/
and change it to
/*Template Name: comments_page*/
Now open your WordPress admin area and create a new page. Call it whatever you want but don't add any content. In the right hand column, select the template that the page uses from the "Page Template" drop down menu. Select 'comments_page' (or whatever you listed as the template name). This tells WordPress to use your file to show this specific page instead of the default page template. Save the page and note the page_id that WordPress generates.
Now, find your theme's comments template, usually 'comments.php'. Find the function wp_list_comments();. We are going to add the name of a custom function that will control the display of your comments as an argument to this function. For an example, go to the twenty-ten theme's files, open comments.php and you'll see what that looks like:
wp_list_comments( array( 'callback' => 'twentyten_comment' ) );
Open the twenty-ten theme's functions.php and find
function twentyten_comment()
Copy that entire function and paste it into your theme's functions file. Change the name to' my_comment()', and add that to the wp_list_comments function call like this:
wp_list_comments( array('callback'=>'my_comment'));
In your newly-created 'my_comment()' function in your functions.php file, add a link where you want to the separate page of comments (comments_page.php) using get_page_link() and a query var named 'commentID' and the comments ID.
View this comment
Now to inappropriately add php logic to a template file. Once you understand how this works, you can create a function in your functions.php file and then call it in the theme file...
On comments_page.php ,use $_GET['commentID'] to retrieve the comment's id value from the url, and pass it to get_comment($commentID) to retrieve the single comment and display it on a single page.
if(isset($_GET['commentID'])){$commentID = $_GET['commentID'];}
$comment = get_comment($commentID);
Now you have all the single comments information in the $comment variable as an object.
You can decide how to display the comment, but to start, I recommend copying the contents of your theme's comments template to keep things consistent. It will show exactly the same thing the post page shows, but it sounds like this page is intended more for the permalink to a single comment that you link to from somewhere else.
Hope this helps. Let me know if you run into a snag.
Note: this answer provides info given to me from Todd Perkins over at wordpress.stackexchange.com
This is my functions.php
<?php
if ( ! function_exists( 'twentyten_comment' ) ) :
function my_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard">
<?php echo get_avatar( $comment, 40 ); ?>
<?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author .vcard -->
<?php if ( $comment->comment_approved == '0' ) : ?>
<em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<?php
/* translators: 1: date, 2: time */
printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
?>
</div><!-- .comment-meta .commentmetadata -->
<div class="comment-body"><?php comment_text(); ?></div>
View this comment
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</div><!-- #comment-## -->
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
This is my comments_page.php
/*Template Name: comments_page*/
<? if(isset($_GET['commentID'])){$commentID = $_GET['commentID'];}
$comment = get_comment($commentID);
?>
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<!--uncomment for header tags-- <h1><?php the_title(); ?></h1>
<small><b>Posted:</b> <?php the_time('F jS, Y') ?> | <b>Author:</b> <?php the_author_posts_link(); ?> | <b>Filed under:</b> <?php the_category(', ') ?> <?php the_tags(' | <b>Tags:</b> ', ', ', ''); ?> <?php if ( $user_ID ) :
?> | <b>Modify:</b> <?php edit_post_link(); ?> <?php endif; ?>| <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></small> -->
<?php the_content('Read the rest of this entry »'); ?>
<hr/>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is my comments.php
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
}
/* This variable is for alternating comment background */
$oddcomment = 'class="alt" ';
?>
<!-- You can start editing here. -->
<div id="comments">
<?php if ($comments) : ?>
<h3><?php comments_number('No Comments', 'One Comment', '% Comments' );?> on “<?php the_title(); ?>”</h3>
<?php wp_list_comments( array('callback'=>'my_comment')); ?>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
<?php if ('open' == $post->comment_status) : ?>
<hr/>
<h4 class="center">Leave a Reply</h4>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be logged in to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<ul class="formlist">
<?php if ( $user_ID ) : ?>
<p>Logged in as <?php echo $user_identity; ?>. Log out »</p>
<?php else : ?>
<li><input type="text" name="author" id="author" value="Name <?php if ($req) echo "(required)"; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> onblur="if(this.value.length == 0) this.value='Name <?php if ($req) echo "(required)"; ?>';" onclick="if(this.value == 'Name <?php if ($req) echo "(required)"; ?>') this.value='';" /></li>
<li><input type="text" name="email" id="email" value="Mail (will not be published) <?php if ($req) echo "(required)"; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> onblur="if(this.value.length == 0) this.value='Mail (will not be published) <?php if ($req) echo "(required)"; ?>';" onclick="if(this.value == 'Mail (will not be published) <?php if ($req) echo "(required)"; ?>') this.value='';" /></li>
<li><input type="text" name="url" id="url" value="Website" size="22" tabindex="3" onblur="if(this.value.length == 0) this.value='Website';" onclick="if(this.value == 'Website') this.value='';" /></li>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<li><textarea name="comment" id="comment" cols="70%" rows="10" tabindex="4" value="Enter comment here."></textarea></li>
<li class="submitbutton"><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" /></li>
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
<?php do_action('comment_form', $post->ID); ?>
</ul>
</form>
<?php endif; // If registration required and not logged in ?>
<?php endif; // if you delete this the sky will fall on your head ?>
</div>

Resources