I'm having problems with adding a custom settings page for wordpress, and more specifically getting the options to save.
The documentation for this seems to be lacking and inconsistent, as different examples and different different functions explain the same thing differently.
The newest example on the wordpress codex uses the sanitize callback function to save options. But the add_settings_field() documentation says that saving the options should happen behind the scenes. Seems wierd to use san callback to save anyways.
I have tried various different approaches, and at one time my code managed to save one of the fields but not all of them.
My current code:
class wwtkSettings {
public function __construct() {
if ( is_admin() ){
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
}
public function add_plugin_page(){
add_options_page( 'WWTK Settings', 'WWTK settings', 'manage_options', 'wwtk-settings', array( $this, 'create_admin_page' ) );
}
public function create_admin_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>WWTK Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'wwtk-setting-group' );
do_settings_sections( 'wwtk-settings' );
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function page_init() {
register_setting( 'wwtk-setting-group', 'wwtk-setting-group', array( $this, 'wwtk_validate_options' ) );
add_settings_section(
'wwtk-settings-section',
'Categories and pages',
array( $this, 'print_section_info' ),
'wwtk-settings'
);
add_settings_field(
'frontpage',
'Frontpage category:',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'frontpage'
);
add_settings_field(
'category1',
'Category & page 1',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category1'
);
add_settings_field(
'category2',
'Category & page 2',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category2'
);
add_settings_field(
'category3',
'Category & page 3',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category3'
);
}
//TODO: check if category and page exist. and sanitize!
public function wwtk_validate_options( $input ) {
return $input;
}
public function print_section_info(){
print 'Enter the names of categories that should be displayed on the page with the same name(both must exist):';
}
public function create_category_field($args){
?><input type="text" id="<?php echo $args ?>" name="wwtk-setting-group" value="<?php echo get_option( 'wwtk-setting-group['.$args.']' ); ?>" /><?php
}
}
/* code similar to your example */
class MySettingsPage{
/**
* Holds the values to be used in the fields callbacks
*/
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',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'id_number', // ID
'ID Number', // Title
array( $this, 'id_number_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-setting-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( !is_numeric( $input['id_number'] ) )
$input['id_number'] = '';
if( !empty( $input['title'] ) )
$input['title'] = sanitize_text_field( $input['title'] );
return $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 id_number_callback()
{
printf(
'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
esc_attr( $this->options['id_number'])
);
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_option_name[title]" value="%s" />',
esc_attr( $this->options['title'])
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();
Related
This function adds a tab named "Special Page" into "My Account" tab list:
add_filter( 'woocommerce_account_menu_items' , 'jc_menu_panel_nav' );
function jc_menu_panel_nav() {
$items = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'downloads' => __( 'Downloads', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Account Details', 'woocommerce' ),
'special-page' => __( 'Special Page', 'woocommerce' ), // My custom tab here
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $items;
}
That results in this:
But the link points to my-account/special-page/, and naturally gives a 404 error.
How I can assign this URL to a file named special-page.php?
Finally I could solve the problem using a snippet provided for the same people of WooCommerce (There are more tips in that page). For anyone interested, paste all the following code in functions.php:
function my_custom_endpoints() {
add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'special-page';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
function my_custom_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );
I think this way allows more control to order/renaming the menu:
function my_custom_my_account_menu_items( $items ) {
$items = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
//'downloads' => __( 'Downloads', 'woocommerce' ),
//'edit-address' => __( 'Addresses', 'woocommerce' ),
//'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Edit Account', 'woocommerce' ),
'special-page' => 'Special Page',
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
In the following function I included the file to maintain some "order", but it also admits direct code.
Be sure to place the special-page.php file in the myaccount folder.
function my_custom_endpoint_content() {
include 'woocommerce/myaccount/special-page.php';
}
add_action( 'woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content' );
Important: Once did this, go to Dashboard > Settings > Permalinks and click "Save Settings" in order to flush rewrite rules (thanks #optimiertes)
Source: Tabbed My Account page
First my-account/special-page/ should be myaccount/special-page/ in woocommerce 2.6+.
This solution is Incomplete and I am still working On…
You can use first this hook:
add_action( 'init', 'add_wc_endpoint' );
function add_wc_endpoint(){
add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
}
Then filtering wc_get_templateto call your files when the request match your endpoint:
add_filter( 'wc_get_template', 'custom_vc_endpoint', 10, 5 );
function custom_vc_endpoint($located, $template_name, $args, $template_path, $default_path){
if( $template_name == 'myaccount/special-page.php' ){
global $wp_query;
if(isset($wp_query->query['special-page'])){
$located = get_template_directory() . '/woocommerce/myaccount/special-page.php';
}
}
return $located;
}
If you use a child theme, replace get_template_directory() by get_stylesheet_directory()… Paste this code in function.php file of your active child theme or theme.
To avoid a 404 error "page not found", you will need to refresh rewrite rules adding to your code:
flush_rewrite_rules();
Update: Finally Dario (the OP) found a working solution. Look at his answer.
References:
Tabbed My Account page (Official Woocommerce 2.6+): Creating new endpoints
How to add a new endpoint in woocommerce (old and incomplete)
There is a better way to use a template in your custom page in woocommerce:
function my_custom_endpoint_content() {
wc_get_template( 'myaccount/special-page.php' );
}
add_action( 'woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content' );
this should work without using the wc_get_template filter.
You can add this code to your theme's function.php:
class My_Custom_My_Account_Endpoint {
/**
* Custom endpoint name.
*
* #var string
*/
public static $endpoint = 'special-page';
/**
* Plugin actions.
*/
public function __construct() {
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Change the My Accout page title.
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/**
* Set endpoint title.
*
* #param string $title
* #return string
*/
public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'Special Page', 'woocommerce' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'Special Page', 'woocommerce' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() {
include('woocommerce/myaccount/special-page.php');
}
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function install() {
flush_rewrite_rules();
}
}
new My_Custom_My_Account_Endpoint();
// Flush rewrite rules on plugin activation.
register_activation_hook( __FILE__, array( 'My_Custom_My_Account_Endpoint', 'install' ) );
If you don't know where is your theme's function.php:
1.Log in to the WordPress Admin interface
2.In the left sidebar, hover over Appearances, then click Theme Editor
3.In the right sidebar, click functions.php
Im currently trying to assemble a plugin for my client that allows them to pull a list of address from a custom-post-type then select which one they would like to ship to then add that to woocommerce's shipping fields, and also make it work with multiple shipping addresses.
I've attached my code below, i've hit a road block when it comes to getting the info into from PHP to the JS and breaking it down to output to the proper fields and output in HTML.
<?php
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'WC_MultiAddress' ) ) {
/**
* Localisation
**/
load_plugin_textdomain( 'WC_MultiAddress', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
class WC_MultiAddress {
public function __construct() {
// called only after woocommerce has finished loading
add_action( 'woocommerce_init', array( &$this, 'woocommerce_loaded' ) );
// called after all plugins have loaded
add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
// called just before the woocommerce template functions are included
add_action( 'init', array( &$this, 'include_template_functions' ), 20 );
// indicates the user is in the right page
if(is_page('checkout')){
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
unset($fields['address']['address_1']);
unset($fields['address']['address_2']);
return $fields;
}
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_address'] = array(
'label' => __('Shipping Address', 'woocommerce'),
'placeholder' => _x('Add Full Street Address Here', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-control'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Shipping Address').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_address', true ) . '</p>';
}
// indicates we are being served over ssl
if ( is_ssl() ) {
$shippings = array();
$args = array('post-type'=>'addressbook');
$query = New WP_Query($args);
if($query->have_posts()):while($query->have_posts()):$query->the_post();
$temp = array();
$temp['id'] = get_the_id();
$temp['fname'] = the_field('fname');
$temp['lname'] = the_field('lname');
$temp['email'] = the_field('email');
$temp['company'] = the_field('company');
$temp['addr1'] = the_field('address_line_1');
$temp['addr2'] = the_field('address_line_2');
$temp['city'] = the_field('city');
$temp['state'] = the_field('state');
$temp['zip'] = the_field('zip');
$shipping[] = $temp;
endwhile;endif;wp_reset_postdata();?>
<select id="address-select" class="form-control">
<?php foreach($shippings as $shipping){
extract($shipping);?>
<option value="<?php echo $id; ?>"><?php echo wp_trim_words($fname, 1) . $lname . ' ' . $company . ' ' . $addr1 . ' ' . $city . ' ' . $state;?></option>
<?php
}
</select>
<Script>
$("#address-select").on("change", function(){
//Code here
})
</Script>
<?php
}
add_filter( 'woocommerce_checkout_fields', 'custom_edit_checkout_fields' );
function custom_edit_checkout_fields( $fields ) {
// Change all attributes on a field
$fields['shipping']['shipping_address_1'] = array(
'label' => __('Shipping Address', 'woocommerce'),
'placeholder' => _x('123 Main St', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-control'),
'clear' => true,
'value' => $addr1
);
return $fields;
}
}
// take care of anything else that needs to be done immediately upon plugin instantiation, here in the constructor
}
/**
* Take care of anything that needs woocommerce to be loaded.
* For instance, if you need access to the $woocommerce global
*/
public function woocommerce_loaded() {
// ...
}
/**
* Take care of anything that needs all plugins to be loaded
*/
public function plugins_loaded() {
// ...
}
/**
* Override any of the template functions from woocommerce/woocommerce-template.php
* with our own template functions file
*/
public function include_template_functions() {
include( 'woocommerce-template.php' );
}
}
// finally instantiate our plugin class and add it to the set of globals
$GLOBALS['WC_MultiAddress'] = new WC_MultiAddress();
}
}
This function adds a tab named "Special Page" into "My Account" tab list:
add_filter( 'woocommerce_account_menu_items' , 'jc_menu_panel_nav' );
function jc_menu_panel_nav() {
$items = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'downloads' => __( 'Downloads', 'woocommerce' ),
'edit-address' => __( 'Addresses', 'woocommerce' ),
'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Account Details', 'woocommerce' ),
'special-page' => __( 'Special Page', 'woocommerce' ), // My custom tab here
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $items;
}
That results in this:
But the link points to my-account/special-page/, and naturally gives a 404 error.
How I can assign this URL to a file named special-page.php?
Finally I could solve the problem using a snippet provided for the same people of WooCommerce (There are more tips in that page). For anyone interested, paste all the following code in functions.php:
function my_custom_endpoints() {
add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'special-page';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
function my_custom_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );
I think this way allows more control to order/renaming the menu:
function my_custom_my_account_menu_items( $items ) {
$items = array(
'dashboard' => __( 'Dashboard', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
//'downloads' => __( 'Downloads', 'woocommerce' ),
//'edit-address' => __( 'Addresses', 'woocommerce' ),
//'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
'edit-account' => __( 'Edit Account', 'woocommerce' ),
'special-page' => 'Special Page',
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
In the following function I included the file to maintain some "order", but it also admits direct code.
Be sure to place the special-page.php file in the myaccount folder.
function my_custom_endpoint_content() {
include 'woocommerce/myaccount/special-page.php';
}
add_action( 'woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content' );
Important: Once did this, go to Dashboard > Settings > Permalinks and click "Save Settings" in order to flush rewrite rules (thanks #optimiertes)
Source: Tabbed My Account page
First my-account/special-page/ should be myaccount/special-page/ in woocommerce 2.6+.
This solution is Incomplete and I am still working On…
You can use first this hook:
add_action( 'init', 'add_wc_endpoint' );
function add_wc_endpoint(){
add_rewrite_endpoint( 'special-page', EP_ROOT | EP_PAGES );
}
Then filtering wc_get_templateto call your files when the request match your endpoint:
add_filter( 'wc_get_template', 'custom_vc_endpoint', 10, 5 );
function custom_vc_endpoint($located, $template_name, $args, $template_path, $default_path){
if( $template_name == 'myaccount/special-page.php' ){
global $wp_query;
if(isset($wp_query->query['special-page'])){
$located = get_template_directory() . '/woocommerce/myaccount/special-page.php';
}
}
return $located;
}
If you use a child theme, replace get_template_directory() by get_stylesheet_directory()… Paste this code in function.php file of your active child theme or theme.
To avoid a 404 error "page not found", you will need to refresh rewrite rules adding to your code:
flush_rewrite_rules();
Update: Finally Dario (the OP) found a working solution. Look at his answer.
References:
Tabbed My Account page (Official Woocommerce 2.6+): Creating new endpoints
How to add a new endpoint in woocommerce (old and incomplete)
There is a better way to use a template in your custom page in woocommerce:
function my_custom_endpoint_content() {
wc_get_template( 'myaccount/special-page.php' );
}
add_action( 'woocommerce_account_special-page_endpoint', 'my_custom_endpoint_content' );
this should work without using the wc_get_template filter.
You can add this code to your theme's function.php:
class My_Custom_My_Account_Endpoint {
/**
* Custom endpoint name.
*
* #var string
*/
public static $endpoint = 'special-page';
/**
* Plugin actions.
*/
public function __construct() {
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Change the My Accout page title.
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/**
* Set endpoint title.
*
* #param string $title
* #return string
*/
public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'Special Page', 'woocommerce' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'Special Page', 'woocommerce' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() {
include('woocommerce/myaccount/special-page.php');
}
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function install() {
flush_rewrite_rules();
}
}
new My_Custom_My_Account_Endpoint();
// Flush rewrite rules on plugin activation.
register_activation_hook( __FILE__, array( 'My_Custom_My_Account_Endpoint', 'install' ) );
If you don't know where is your theme's function.php:
1.Log in to the WordPress Admin interface
2.In the left sidebar, hover over Appearances, then click Theme Editor
3.In the right sidebar, click functions.php
Here is a code to create, define and rename/reorder custom endpoint for Woocommerce My Account page. Everything was fine until I updated Woocommerce and Avada. Now gives an ERROR 404 page when clicking on the new menu item, the URL being:
http://pplkonyv.hu/my-account/my-custom-endpoint
What should I modify?
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
/**
* Flush rewrite rules on theme activation.
*/
function my_custom_flush_rewrite_rules() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_custom_flush_rewrite_rules' );
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
function my_custom_my_account_menu_items( $items ) {
// Insert your custom endpoint.
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
/**
* Endpoint HTML content.
*/
function my_custom_endpoint_content() {
echo ( '<h2>Előfizetések</h2>' );
echo do_shortcode( '[ld_profile]' );
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );
function wpb_woo_my_account_order() {
$myorder = array(
'edit-account' => __( 'Felhasználónév és jelszó', 'woocommerce' ),
'edit-address' => __( 'Címek', 'woocommerce' ),
'orders' => __( 'Megrendelések', 'woocommerce' ),
'my-custom-endpoint' => __( 'Előfizetések', 'woocommerce' ),
'customer-logout' => __( 'Logout', 'woocommerce' ),
);
return $myorder;
}
add_filter ( 'woocommerce_account_menu_items', 'wpb_woo_my_account_order' );
I have an issue with this WooCommerce Grid / List toggle plugin.
On my clients website price of products is positioned with css position: absolute;. It works fine in grid view. But when I switch to list view, the price is overlapping other text. I tried to edit position of price (with top: 10px; forexample) but it changed also the position of price in grid view.
So I want to use php if statement in header and echo the proper position in <style> according to grid view or list view was chosen.
The problem I have is that I don't know which function should I use. Or which variables should I compare, according to which view was chosen.
Here is the code of plugin:
<?php
/*
Plugin Name: WooCommerce Grid / List toggle
Plugin URI: http://jameskoster.co.uk/tag/grid-list-toggle/
Description: Adds a grid/list view toggle to product archives
Version: 0.4.0
Author: jameskoster
Author URI: http://jameskoster.co.uk
Requires at least: 3.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: wc_list_grid_toggle
Domain Path: /languages/
*/
/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
/**
* Localisation
**/
load_plugin_textdomain( 'wc_list_grid_toggle', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/**
* WC_List_Grid class
**/
if (!class_exists('WC_List_Grid')) {
class WC_List_Grid {
public function __construct() {
// Hooks
add_action( 'wp' , array( $this, 'setup_gridlist' ) , 20);
// Init settings
$this->settings = array(
array(
'name' => __( 'Default catalog view', 'wc_list_grid_toggle' ),
'type' => 'title',
'id' => 'wc_glt_options'
),
array(
'name' => __( 'Default catalog view', 'wc_list_grid_toggle' ),
'desc_tip' => __( 'Display products in grid or list view by default', 'wc_list_grid_toggle' ),
'id' => 'wc_glt_default',
'type' => 'select',
'options' => array(
'grid' => __('Grid', 'wc_list_grid_toggle'),
'list' => __('List', 'wc_list_grid_toggle')
)
),
array( 'type' => 'sectionend', 'id' => 'wc_glt_options' ),
);
// Default options
add_option( 'wc_glt_default', 'grid' );
// Admin
add_action( 'woocommerce_settings_image_options_after', array( $this, 'admin_settings' ), 20 );
add_action( 'woocommerce_update_options_catalog', array( $this, 'save_admin_settings' ) );
add_action( 'woocommerce_update_options_products', array( $this, 'save_admin_settings' ) );
}
/*-----------------------------------------------------------------------------------*/
/* Class Functions */
/*-----------------------------------------------------------------------------------*/
function admin_settings() {
woocommerce_admin_fields( $this->settings );
}
function save_admin_settings() {
woocommerce_update_options( $this->settings );
}
// Setup
function setup_gridlist() {
if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'setup_scripts_styles' ), 20);
add_action( 'wp_enqueue_scripts', array( $this, 'setup_scripts_script' ), 20);
add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30);
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'gridlist_buttonwrap_open' ), 9);
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'gridlist_buttonwrap_close' ), 11);
add_action( 'woocommerce_after_shop_loop_item', array( $this, 'gridlist_hr' ), 30);
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_excerpt', 5);
add_action( 'woocommerce_after_subcategory', array( $this, 'gridlist_cat_desc' ) );
}
}
// Scripts & styles
function setup_scripts_styles() {
if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
wp_enqueue_style( 'grid-list-layout', plugins_url( '/assets/css/style.css', __FILE__ ) );
wp_enqueue_style( 'grid-list-button', plugins_url( '/assets/css/button.css', __FILE__ ) );
}
}
function setup_scripts_script() {
if ( is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy() ) {
wp_enqueue_script( 'cookie', plugins_url( '/assets/js/jquery.cookie.min.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'grid-list-scripts', plugins_url( '/assets/js/jquery.gridlistview.min.js', __FILE__ ), array( 'jquery' ) );
add_action( 'wp_footer', array(&$this, 'gridlist_set_default_view') );
}
}
// Toggle button
function gridlist_toggle_button() {
?>
<nav class="gridlist-toggle">
⊞ <span><?php _e('Zobrazit mřížku', 'wc_list_grid_toggle'); ?></span>⊟ <span><?php _e('Zobrazit seznam', 'wc_list_grid_toggle'); ?></span>
</nav>
<?php
}
// Button wrap
function gridlist_buttonwrap_open() {
?>
<div class="gridlist-buttonwrap">
<?php
}
function gridlist_buttonwrap_close() {
?>
</div>
<?php
}
// hr
function gridlist_hr() {
?>
<hr />
<?php
}
function gridlist_set_default_view() {
$default = get_option( 'wc_glt_default' );
?>
<script>
if (jQuery.cookie('gridcookie') == null) {
jQuery('ul.products').addClass('<?php echo $default; ?>');
jQuery('.gridlist-toggle #<?php echo $default; ?>').addClass('active');
}
</script>
<?php
}
function gridlist_cat_desc( $category ) {
global $woocommerce;
echo '<div itemprop="description">';
echo $category->description;
echo '</div>';
}
}
$WC_List_Grid = new WC_List_Grid();
}
}
Finaly I solved this issue. I used the CSS class: ul.products.list li.product .price to change the appearance of the price in list view. The grid view is probably default ul.products li .price.
Now all works. Hope this will help also someone who has also problem with it.