Woocommerce: redirect (keep) user in cart page after log-in in cart page - wordpress

By default, Woocommerce displays the user login box in the checkout page; I wanted it to appear on the cart page as well, so I have added the following to the functions.php file in my theme's directory:
// display login form in cart page
add_action( 'woocommerce_after_cart', 'woocommerce_login_form' );
This works, but after the user log-in, he is redirected to his account page; I would like him to be kept at the cart page, which I think can be done by passing a redirect argument, as per the function's architecture:
function woocommerce_login_form( $args = array() ) {
$defaults = array(
'message' => '',
'redirect' => '',
'hidden' => false
);
$args = wp_parse_args( $args, $defaults );
wc_get_template( 'global/form-login.php', $args );
}
How can I pass an url as an argument to this function?
Thank you for your help.

I have managed to do it be replicating the function with some changes:
// display login form in cart page
add_action( 'woocommerce_after_cart', 'woocommerce_login_form_in_cart' );
function woocommerce_login_form_in_cart( $args = array() ) {
$defaults = array(
'message' => '',
'redirect' => '#',
'hidden' => false
);
$args = wp_parse_args( $args, $defaults );
wc_get_template( 'global/form-login.php', $args );
}

Related

Render WooCommerce Handpicked Products Block from a plugin

In the plugin code I have:
add_action( 'wp_loaded', 'show_widget');
function show_widget(){
$block_name = 'woocommerce/handpicked-products';
$converted_block = new WP_Block_Parser_Block( $block_name, array(
'query' => new WP_Query( array (
'post__in' => $products, // $products is a given array of product IDs
'post_type' => 'product'
) )
), array(), '', array() );
$serialized_block = serialize_block( (array) $converted_block );
echo $serialized_block;
}
as a result, i see a commented out wp:query when I view source:
yet nothing is actually painted on the page. Notice there is one product in the result set and I expected it to be rendered to the screen.
Why doesn't it? What am I missing?
switched serialize_block with render_block and it works...

WordPress XMLRPC - Search Post by Slug and Update

Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers
I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );

How do I add a new page using code thru a Wordpress plugin?

I followed the chosen answer here -> How to create new page in wordpress plugin?
and I added the following code in a new Wordpress plugin folder and file and then activated in the Wordpress admin menu. Yet I don't have a new page created when I go to the slug demosite.com/custom/
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page('custom_page', 'custom', 'administrator','custom', 'custompage');
remove_menu_page('custom');
}
Do I have to do something special to make my Wordpress plugin code work? I really need to be able to add a new page using my plugin functionality.
For create fronted page when plugin activation used register_activation_hook() like below.
register_activation_hook() function registers a plugin function to be run when the plugin is activated.
The first thing we do on activation is check that the current user is allowed to activate plugins. We do this using the current_user_can function
Finally, we create our new page, after we check that a page with the same name does not exist
register_activation_hook( __FILE__, 'register_newpage_plugin_activation' );
function register_newpage_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function
After plugin active successfully you can access your page using demosite.com/new-page-slug/
I'm not sure if you intended for the page to be created only once if so you should do it during plugin activation.
You might want to consider the following pseudo-ish code:
register_activation_hook( __FILE__, 'moveFile' );
function moveFile(){
if( check if post exists ){
wp_insert_post() # obviously title is "whatever", following convention
#move the file to themes folder
$source = plugin_dir_path(__FILE__) . "page-whatever.php";
$destination = get_template_directory() . "/page-whatever.php";
$cmd = 'cp ' . $source . ' ' . $destination;
exec($cmd);
}
}
It's similar to the code answered by Ankur, but this sample let you have a custom page. Caveat, my method uses exec() command.
I hope this helps.
/* If you want create a page with syn page template on plugin activation so see below example */
register_activation_hook( __FILE__, 'activate' );
function activate() {
$the_slug = 'our-services';
$args = array(
'name' => $the_slug,
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Our Services',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'our-services'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/our-services.php' );
}
}
/* page-template - our-services.php* /
<?php
/*
* Template Name: our-services
*/
get_header();
get_footer();
?>

How to delete page after plugin deactivation

I have created page when my plugin is activated. Its working fine. Now i want to delete the page when my plugin is deactivated.
My code is given below :
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function() {
$post = array('page_template' => '', 'comment_status' => 'closed', 'ping_status' => 'closed' ,'post_author' => 1,'post_date' => date('Y-m-d H:i:s'),'post_name' => 'Checklists','post_status' => 'publish' ,
'post_title' => 'Checklists',
'post_type' => 'page',
);//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue ); }
register_deactivation_hook( __FILE__, 'my_plugin_remove' );
function my_plugin_remove() {// the id of our page...
$the_page_id = get_option( $newvalue );
if( $the_page_id ) {
wp_delete_post( $the_page_id ); // this will trash, not delete
}
How can I get the post id to delete the page?
wp_delete_post( $the_page_id, true );
The second parameter is to "force deletion", is boolean, and when set to true it deletes the post without trashing it.
You can read more in the docs
You can get the ID using the get_option function:
get_option('hclpage');

Update wordpress post from front-end (using acf)

I am making front end admin for users to add/edit their posts. I already made post add form and it works, but edit form doesnt work.
functions.php
function add_new_post( $post_id )
{
if( $post_id == 'new' ) {
// Create a new post
$post = array(
'post_title' => $_POST["fields"]['field_52c810cb44c7a'],
'post_category' => array(4),
'post_status' => 'draft',
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
return $post_id;
}
else {
return $post_id;
}
}add_filter('acf/pre_save_post' , 'add_new_post' );
index.php
<div id="updateform-<?php the_ID(); ?>" class="collapse">
<?php
echo get_the_ID();
$args = array(
'post_id' => get_the_ID(), // post id to get field groups from and save data to
'field_groups' => array(31), // this will find the field groups for this post (post ID's of the acf post objects)
'form' => true, // set this to false to prevent the <form> tag from being created
'form_attributes' => array( // attributes will be added to the form element
'id' => 'post',
'class' => '',
'action' => get_permalink( get_the_ID() ),
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
'html_before_fields' => '', // html inside form before fields
'html_after_fields' => '', // html inside form after fields
'submit_value' => 'Update', // value for submit field
'updated_message' => 'Post updated.', // default updated message. Can be false to show no message
);
acf_form( $args );
?>
</div>
For saving posts you should use save_post not pre_save_post.
Basically pre_save_post is used for new posts.
Use this below add_filter('save_post' , 'add_new_post');
I created plugins for that:
Forms actions
https://wordpress.org/plugins/forms-actions/
Add actions to yours ACF forms.
ACF Frontend display
https://wordpress.org/plugins/acf-frontend-display/
Display your ACF form on frontend
If by some chance you happen to be using Elementor Page Builder, you can do this by installing Advanced Widgets for Elementor. It comes with an ACF Form widget that you can just drag & drop anywhere into your site and configure your form through the UI (no coding required).

Resources