How to add new Custom Title in Woocommerce My Account sidebar? - php

Hi i want to add new custom title and links into My Account page into my WooCommerce site.. Searched all documentation, and Stackoverflow topics, but not found sollution for my request.
This is title for what im asking for
Text Muj Ucet is My Account in english. :)
i want to add new title like is shown in image bellow:
This is WooCommerce Template Code for that part:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<p><?php
/* translators: 1: user display name 2: logout url */
printf(
__( 'Hello %1$s (not %1$s? Log out)', 'woocommerce'
),
'<strong>' . esc_html( $current_user->display_name ) . '</strong>',
esc_url( wc_logout_url( wc_get_page_permalink( 'myaccount' ) ) )
);
?></p>
<p>Na nástěnce svého uživatelského účtu si můžete stáhnout své zakoupené
produkty a faktury, upravit své osobní informace, změnit heslo nebo
fakturační adresu.</p>
<?php
/**
* My Account dashboard.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_dashboard' );
/**
* Deprecated woocommerce_before_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_before_my_account' );
/**
* Deprecated woocommerce_after_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_after_my_account' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already
sent" issues. */
I want to add new one title bellow that sidebar. How to register a new title ?
Thanks

Here is the solution.
add_filter('woocommerce_account_menu_items', 'display_account_new_link');
function display_account_new_link( $items ) {
$items['new_link'] = __( 'New Link', 'text-domain' );
return $items;
}
add_action( 'woocommerce_account_new_link_endpoint', 'new_account_link_content' );
function new_account_link_content() {
//include your display template here
echo "Here goes you content";
}
After pasting this code in your plugin or in your theme function.php file this code will make a new link in the my account navigation sidebar, along with the template you want to assign this link. Here new_link is the slug for this navigation link. If you want to give some different slug you must rename new_link written everywhere in the given code. As soon as you click on this New Link it will redirect you to the Page Not Found Page. It can be solved by adding this code.
add_action( 'init', 'register_new_link_endpoint');
function register_new_link_endpoint() {
add_rewrite_endpoint( 'new_link', EP_PAGES );
}
After pasting this code you must save your permalink once, by going to WordPress Dashboard->Settings->Permalinks and hit the save changes button.

Related

Wordpress custom demo sidebar not appearing on posts

Following php file gets a custom demo sidebar to show up in the admin widget menu, but not on actual posts (file located in folder with same name, which is located in the plugin folder in WP file directory) – add a text widget to custom sidebar to test:
<?php
/**
* Plugin Name: Single Post CTA
* Plugin URI: https://github.com/cdils/single-post-cta
* Description: Adds sidebar (widget area) to single posts
* Version: 0.1
* Author: Carrie Dils
* Author URI: https://carriedils.com
* License: GPL v2+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: spc
*/
// If this file is called directly, abort
if ( !defined( 'ABSPATH' ) ) {
die;
}
/**
* Load stylesheet
*/
function spc_load_stylesheet() {
if ( is_single() ) {
wp_enqueue_style( 'spc_stylesheet', plugin_dir_url(__FILE__) .'spc-styles.css' );
}
}
// Hook stylesheet
add_action( 'wp_enqueue_scripts', 'spc_load_stylesheet' );
// Register a custom sidebar
function spc_register_sidebar() {
register_sidebar( array(
'name' => __( 'Single Post CTA', 'spc' ),
'id' => 'spcsidebar',
'description' => __( 'Displays widget area on single posts', 'spc' ),
'before_widget' => '<div class="widget spc">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle spc-title">',
'after_title' => '</h2>',
) );
}
// Hook sidebar
add_action( 'widgets_init', 'spc_register_sidebar' );
// Display sidebar on single posts
function spc_display_sidebar( $content ) {
if ( is_single() ) {
dynamic_sidebar( 'spcsidebar' );
}
return $content;
}
// Add dynamic sidebar
add_filter( 'the content', 'spc_display_sidebar' );
Here is the associated style sheet located in the same folder as the file for the custom sidebar:
.spc {
background: gray;
color: blue;
}
The widgets menu under customizer says “Your theme has 1 other widget area, but this particular page doesn’t display it”. This WordPress guide https://developer.wordpress.org/themes/functionality/sidebars/ appears to indicate that one has to register the sidebar/widget in the theme or child theme’s functions.php file and then create a sidebar-{name}.php file in which to run the dynamic_sidebar function. Is this the way instead? I’m using the Genesis Sample child theme, and switching to 2020 and 2017 wordpress themes, or deactivating all other plugins has not fixed problem.
The filter should be add_filter( 'the_content', 'spc_display_sidebar' );. You forgot the underscore.
If you are trying to display the sidebar in a page post type thenis_single() is not going to work. Try is_singular()instead.
Mistack is in hook not "the content", use "the_content"
Ex.
add_filter( 'the_content', 'spc_display_sidebar' );

Displaying an admin message on woocommerce_order_action hook

I used this code :
https://gist.github.com/bekarice/5233ed58c3a836064123b290463241c0
In sv_wc_process_order_meta_box_action function, how is possible to display a message box to admin?
Currently code uses update_post_meta() function and add_order_note() method and not display any message to admin.
Thanks.
The only way I know is to use custom functions with the admin_notices action hook. So you could try to include the related add_action() inside the code you are using.
This code is untested, and I don't guarantee anything:
// The message function to be hooked in 'admin_notices' hook.
function my_custom_admin_notice() {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e('Order has been updated "printed for packaging"'); ?></p>
</div>
<?php
}
//The second function that you use (customized with an add_action()):
function sv_wc_process_order_meta_box_action( $order ) {
// add the order note
$message = sprintf( __( 'Order information printed by %s for packaging.', 'my-textdomain' ), wp_get_current_user()->display_name );
$order->add_order_note( $message );
// add the flag so this action won't be shown again
update_post_meta( $order->id, '_wc_order_marked_printed_for_packaging', 'yes' );
// Setting the admin message function in 'admin_notices' hook.
add_action('admin_notices', 'my_custom_admin_notice');
}
add_action( 'woocommerce_order_action_wc_custom_order_action', 'sv_wc_process_order_meta_box_action' );
Related documentation:
Complete Guide to WordPress Admin Notices
How to Add Admin Alerts and Error Messages to the Backend of WordPress

Wordpress Plugin Dev : Fatal error: Call to undefined function add_action()

I'm new on plugin development.
I'm try to create a custom Printable form page in wp-admin to create Customer Postal Address.
very Similar This plugin
when administrator click on "print Address" link , pop-up template.php page with customer address and information for print address
The Problem is :
I get fatal Error when click on print order anchor tag and i can't run any wordpress action on template.php:
Fatal error: Call to undefined function add_action() in C:\xampp\htdocs\wp-content\plugins\address generator\template.php on line 4
<?php
/**
* Plugin Name: Address Generator
* Plugin URI: http://CGTV.ir
* Description:Generate Postal Label for Parcel
* Version: 1.0 or
* Author: Hamed Mayahian
* Author URI: CGTV.ir
* License: A "Slug" license name e.g. GPL12
*/
// ADDING COLUMN TITLES (Here 2 columns)
/*define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
include( MY_PLUGIN_PATH . 'template.php');
*/
require_once(ADDRESS__PLUGIN_DIR .'template.php');
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
function custom_shop_order_column($columns)
{
//add columns
$columns['my-column1'] = __( 'چاپ آدرس','theme_slug');
return $columns;
}
// adding the data for each orders by column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'cbsp_credit_details', 10, 2 );
function cbsp_credit_details( $column )
{
global $post, $woocommerce, $the_order;
$order_id = $the_order->id;
switch ( $column )
{
case 'my-column1' :
$myVarOne = wc_get_order_item_meta( $order_id, '_the_meta_key1', true );
echo $myVarOne;
echo "<a target='_blank' href='".plugins_url( 'template.php' , __FILE__ )."?order=$order_id'>Print Address</a>";
break;
}
}
Template.php
<?php
add_action('init', 'my_init', 1);
function my_init(){
global $post, $woocommerce, $the_order;
$id = $_GET['order'];
$order = new WC_Order($id);
$address = $order->get_billing_address();
$customer_id = get_current_user_id();
if($_GET['order'] == "") {
// no username entered
echo "آدرس پیدا نشد";
} else {
echo "Hello, " . $address;
}
}
?>
Since I don't know what you are trying to accomplish, I can only suggest the following as an improvement in how you are launching your plugin and how you are displaying the custom column.
/**
* Plugin Name: Custom Shop Column Link
* Plugin URI: http://stackoverflow.com/a/39280792/383847
* Description: Link for shop column to display billing address
* Version: 1.0.0
* Author: helgatheviking
* Author URI: http://kathyisawesome.com/
* Text Domain: your-plugin
* Domain Path: /languages
*
* Copyright: © 2015 Kathy Darling and Manos Psychogyiopoulos
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// add all your hooks only when woocommerce has fully loaded it's files
add_action( 'woocommerce_loaded', 'custom_address_generator_init' );
function custom_address_generator_init(){
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
add_action( 'manage_shop_order_posts_custom_column', 'cbsp_credit_details',11);
}
// add your custom column
function custom_shop_order_column($columns)
{
//add columns
$columns['my-column1'] = __( 'چاپ آدرس', 'your-plugin');
return $columns;
}
// adding the data for each orders by column (example)
function cbsp_credit_details( $column )
{
global $the_order;
$order_id = $the_order->id;
switch ( $column )
{
case 'my-column1' :
$myVarOne = get_post_meta( $order_id, '_the_meta_key1', true );
echo $myVarOne;
$url = add_query_arg( array( 'order_id' => $order_id, 'my-action' => 'do-something-cool', ), wp_nonce_url( admin_url(), 'my_order_nonce', 'my_nonce' ) );
printf( '<a class="custom-class" href="%s" data-order_id="%s">%s</a>', $url, $order_id, __( 'Print Address', 'your-plugin' ) );
break;
}
}
EDIT 2 We're going to create a link to the front-end so we can load a custom template via template_include. It should have enough security on it to keep it limited to only the appropriate users.
// load a custom template when special link is clicked
add_action( 'template_include', 'my_template', 1 );
function my_template(){
if( isset( $_GET['my-action'] ) && $_GET['my-action'] == 'do-something-cool' && isset( $_GET['order_id'] ) && current_user_can( 'edit_shop_order', $_GET['order_id'] ) && wp_verify_nonce( $_GET['my_nonce'], 'my_order_nonce' ) ){
return untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/my-template.php';
}
}
Then a /templates/my-plugin.php file in your plugin folder:
<?php
$order_id = intval( $_GET['order_id'] );
$order = wc_get_order($order_id);
if( is_a( $order, 'WC_Order' ) ){
$address = $order->get_formatted_billing_address ();
if( $address ){
printf( '%s, %s', __( 'Hello', 'your-plugin' ), $address );
} else {
_e( 'No billing address', 'your-plugin' );
}
} else {
_e( 'Not a valid order ID', 'your-plugin' );
}
I've dropped the my_init() function in favor of my_template() which will now load a custom template (/templates/my-template.php) via the template_include filter. This template is loaded by WordPress and has all the WordPress functions available for you to use.
template.php file is out of Wordpress then we are not access Wordpress core functions. Include this file in main plugin is works properly but when we access by url directly this file then we can't access Wordpress core functions because we are not follow Wordpress stranded. Order list table have button called url generate like something is http://localhost/wp-content/plugins/address%20generator/template.php?order=5147. When we access this get following error "Fatal error: Call to undefined function add_action() in.."
First comment this line in your main plugin file.
// require_once('template.php');
Changes in template.php file.
<?php
require('../../../wp-load.php');
$id = $_GET['order'];
$order = new WC_Order($id);
$address = $order->get_billing_address();
$customer_id = get_current_user_id();
if($_GET['order'] == "") {
// no username entered
echo "آدرس پیدا نشد";
} else {
echo "Hello, " . $address;
}
But this is not a Wordpress stranded solution. User #helgatheviking is provide best solution for this.

customise endpoint urls in woocommerce

I'm trying to create a tabbed version of "My Account" in woocommerce.
I have built my pages in php using bootstrap css and included the wordpress header and footer so the page loads correctly.
The tabs display as expected by I am having an issue with the endpoint urls in woocommerce:
- 'edit-address'
- 'view-order'
These endpoints are generated dynamically and appended to the url: www.site-name.com/my-account/edit-address
Here's the function call (in file .plugins/woocommerce/myaccount/form-edit-address.php):
<a href="<?php echo wc_get_endpoint_url( 'edit-address', $name ); ?>
I've included 'my-addresses' in my tabbed page which dispays ok. However, the link to edit the shipping and billing addresses is generated by the above call to the endpoints. When the link is clicked the page fails to load and returns a 404 error.
My custom php page is basically www.site-name.com/account/edit-address
The problem is:
The 'edit-address' page content is not loading and I get a 404 error
I'm guessing the issue is caused because my pages are external php pages and not stored with wp database?
Is there a way I can customise the endpoint url so it appends correctly and loads the page?
Link to the page on my site: www.thecookerytutor dot co dot uk/account
Origional woocommerce my-account page: www.thecookerytutor dot co dot uk/my-account
(You will need to create a login to view both pages)
It's had me stumped for days!
Thanks in advance.
Code for my addresses tab on my custom php page as promised...
echo "<div id = 'edit-addresses' class='tab-pane fade'>";
echo "<div id = 'content' class = 'page col-full'>";
echo "<section id = 'main' class = 'col-left'>";
echo "<BR>";
include "my-address.php";
echo "</section>";
echo "</div>";
echo "</div>";`
As you can see I'm including the wc page that loads the addresses (I've copied to the local folder)
Here's the content of edit-address.php (I've copied to my local folder and added wp header). The page loads the header but the get address function fails at line 50.
<?php
define('WP_USE_THEMES', false);
require('../wp-blog-header.php');
add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );
function wp_title_so_18381106( $title, $sep, $seplocation )
{
return 'Your Account | ';
}
get_header();
?>
<?php
/**
* Edit address form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $current_user;
$page_title = ( $load_address === 'billing' ) ? __( 'Billing Address', 'woocommerce' ) : __( 'Shipping Address', 'woocommerce' );
get_currentuserinfo();
?>
<?php wc_print_notices(); ?>
<?php //if ( ! $load_address ) : ?>
<?php //wc_get_template( 'myaccount/my-address.php' ); ?>
<?php //else : ?>
<form method="post">
<h3><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title ); ?></h3>
<?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?>
/fails to load here/
<?php foreach ( $address as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : $field['value'] ); ?>
<?php endforeach; ?>
<?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?>
<p>
<input type="submit" class="button" name="save_address" value="<?php _e( 'Save Address', 'woocommerce' ); ?>" />
<?php wp_nonce_field( 'woocommerce-edit_address' ); ?>
<input type="hidden" name="action" value="edit_address" />
</p>
</form>
<?php //endif; ?>
WooCommerce is a plugin for WordPress, you can't use it without WordPress... for example /my-account/edit-address/, that my-account is actually the page you are rendering and edit-address is just a variable. edit-address was added as an endpoint to my-account. you need to do the same for your account.
You should get an idea of what endpoints are.
Other thoughts about this. If you really want to use account and not my-account, look into your pages for "My Account" and change it's permalink. You can also create another page that can use account in it's permalink. Then use that page in your WooCommerce > Settings > Account tab > My Account Page.
If you have some custom php codes, you can just copy the template from your woocommerce plugin to your theme folder and edit the file you need. Read Template Structure + Overriding Templates via a Theme.
And if I have not addressed your problem, comment down below.

Wordpress Plugin Dynamic Error Page

What action or filter can I use in a Wordpress plugin to dynamically replace the contents (i.e. not the header or footer) of a 404 error page?
Basically I'm looking for a 404 error page equivalent for the_content filter, which will filter the contents of an existing page.
Thank you for your time.
Note: I know I can manually modify the 404 error page for the current theme, but that is not the effect I am trying to achieve.
From this WordPress Answer: How to control output of custom post type without modifying theme?
Plugin file:
<?php
/*
Plugin Name: Plugin 404 Page
Plugin URI: http://stackoverflow.com/questions/14539884
Description: Use the plugin's template file to render a custom 404.php
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
Version: 2013.26.01
License: GPLv2
*/
class Universal_Template
{
public function __construct()
{
$this->url = plugins_url( '', __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
add_filter( 'template_include', array( $this, 'template_404' ) );
}
public function template_404( $template )
{
if ( is_404() )
$template = $this->path . '/404.php';
return $template;
}
}
$so_14539884 = new Universal_Template();
And in the plugin folder, a file named 404.php:
<?php
/**
* The template for displaying 404 pages (Not Found).
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
MY 404!
</div><!-- #primary -->
<?php get_footer(); ?>
The solution depends on the content of 404.php file. If this file contains static text, like
_e( 'It seems we can’t find what you’re looking for...', 'twentyeleven' );
you can add your own filter
apply_filters( 'my_404_content', 'Default 404 message' );
and in functions.php (or in plugin)
add_filter( 'my_404_content', 'replace_404_message' );
function replace_404_message($message) {
return 'Error 404 - '.$message;
}
If 404.php uses built-in WP functions to display page content, you should check what filters they are supported.
You might be able to add a the_content filter with a conditional is_404 section:
function content_404($content) {
if (is_404()) {
// do some stuff with $content
}
// no matter what,
return $content;
}
add_filter( 'the_content', 'content_404' );
Note that this does assume that the 404.php page template has a the_content template tag in place.

Categories