Creating a WordPress post publish button on the front end - wordpress

How can I create a button in the frontend that when the user clicks on the button, the post changes from draft to published?
I use the default function to generate the shortcode but it doesn't work
In fact, the post that is currently available and in the draft status in the front form, the user clicks on the button to publish it.
function wp_publish_post( $post ) {
global $wpdb;
$post = get_post( $post );
if ( ! $post ) {
return;
}
if ( 'publish' === $post->post_status ) {
return;
}
$post_before = get_post( $post->ID );
add_shortcode('publish_post','wp_publish_post');
tanks

Related

WordPress plugin to tag category while posting

I'm not a big programmer, but this week get in touch with Openai. It's my first experience with AI. I tried to create code that works as a Wordpress plugin to meet a need.
I make a lot of posts and each post I have to mark the same category. Then I thought that there should be a plugin to mark the category so that when I need to post again, the category would already have its checkbox checked, so I wouldn't need to scroll the selection box of the categories and mark the same category.
Openai generated this code, it didn't work, from what I noticed the cookie is not being created.
Plugin Name: Category Marker
Description: Checks the checkbox of the previously marked category when making a post
Version: 0.1
Author:
*/
//This is the first function
function catemarcada_create_cookie() {
global $post;
if ( isset( $post ) && 'post-new.php' == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
// Create cookie when making a post
if ( isset( $post->ID ) ) {
$categoryID = get_post_meta($post->ID, '_category_id', true);
setcookie( 'catemarcada', $categoryID, time() + (86400 * 30), "/"); // 86400 = 1 day
}
}
}
add_action( 'wp_loaded', 'catemarcada_create_cookie' );
//This is the second function
function catemarcada_get_cookie() {
global $post;
if ( isset( $post ) && 'post-new.php' == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
// Get cookie when accessing the post-new.php file
if ( isset( $_COOKIE['catemarcada'] ) ) {
$categoryfound = $_COOKIE['catemarcada'];
}
}
}
add_action( 'wp_loaded', 'catemarcada_get_cookie' );
//This is the third function
function catemarcada_check_category() {
global $post;
if ( isset( $post ) && 'post-new.php' == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
// Compare the value of the cookie with the category IDs in the WordPress database
$categories = get_categories();
foreach ( $categories as $category ) {
if ( $categoryfound == $category->cat_ID ) {
// Check the checkbox field of the category in the WordPress database
echo '<input type="checkbox" name="'.$category->cat_ID.'" checked="checked" />';
}
}
}
}
add_action( 'wp_loaded', 'catemarcada_check_category' );
// Redirect to post-new.php
add_action('post_updated', 'redirect_post_new', 10, 3);
function redirect_post_new( $post_id, $post, $update ){
if( $update ){
wp_redirect( admin_url( 'post-new.php' ) );
exit();
}
}
This is the description of the plugin I asked to be generated:
Write PHP code that works as a Wordpress plugin. The plugin will only work when I access the “post-new.php” file.
First function:
When making a post, the plug-in will create a cookie with the name “catemarcada”.
This cookie will store the selected category ID during text posting. The cookie must expire within 360 days.
Second function:
When accessing the “post-new.php” file, check if the cookie named “catemarcada” already exists. If it exists, do not create it again.
And every time I access the “post-new.php” file, the plug-in will look up the ID that was recorded in the cookie named “catemarcada” and record the ID in a variable called “categoryfound”.
Third function:
The plug-in should compare the ID that is in the variable called “categoryfound” with the IDs of the categories in the Wordpress database.
If the ID that is in the variable called “categoryfound” is the same as the ID of any of the categories in the Wordpress database, check the checkbox field of the category in the Wordpress database in question.
When making a post, redirect to the post-new.php file.

External product feature image to external link in new tab -wordpress woocommerce

I have a woocommerce store.
Its an affiliate store.
I want that when clicked on the featured image on the shop page it goes to an external website
I have added below code to my function.php its working fine just need to know what code and where is to be added to open it in a new tab
**<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}**
There is no way to redirect in a new tab, I will suggest adding "target='_blank'" to the tag where you display the featured image with probably the same condition as in the function above. The hacky way maybe
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
echo "<script> window.open(" . $product->get_product_url() . ", '_blank') </script>";
}
}
But I strongly suggest you not to go this way!!

Trigger action when new post is insert in wordpress

My question is,
Is there any filter or action that trigger when new post is insert in database..?
The reason behind it is I want to add key in post meta when new post is insert from admin side.
I got Action called "save_post" but after refer link .This action trigger in created and update post.
but I only want to add meta key when post is created not at update time
You can use wp_insert_post so you will get post_id as soon as post inserted and you can then use that to add meta_key.
If you are not using wp_insert_post and want to use action then you can simply put below code :
if ( wp_is_post_revision( $post_id ) )
return;
which means that if you are updating the post, then it will return back from function.
EDITED
Method-1 to achieve it.
You can simply check with the get_post method that post is there or not.something like below:
add_action('save_post', 'check_for_post_in_database');
function check_for_post_in_database($post_id) {
//check if the post is in the database or not with get_post( $post_id ) == null
if( get_post( $post_id ) == null ) {
//your code to add meta
}
}
//You can do same thing with publish_post
Method-2 to achieve it.
add_action('publish_post', 'check_for_meta_in_database');
function check_for_meta_in_database($post_id) {
global $wpdb;
$your_meta = get_post_meta($post_id, 'meta_key', true);
if( empty( $your_meta ) && ! wp_is_post_revision( $post_id ) ) {
update_post_meta($post_id, 'meta_key', 'meta_value');
}
}
But as you said there are many meta there, this method will be bit long.
Method-3 to achieve it.
You can do as rnevius suggested which is the one even I would opt. Its like :
add_action( 'transition_post_status', 'check_transition_and_then_add_meta', 10, 3 );
function check_transition_and_then_add_meta( $new_status, $old_status, $post ) {
if ( ( 'draft' === $old_status || 'auto-draft' === $old_status ) && $new_status === 'publish' ) {
add_post_meta($post->ID, 'your_meta_key', 'your_meta_value');
}
}
or else you can do it with draft_to_publish like:
//as rnevius suggested {$old_status}_to_{$new_status}
add_action( 'draft_to_publish', 'add_meta_when_status_change' );
function add_meta_when_status_change() {
add_post_meta($post->ID, 'your_meta_key', 'your_meta_value');
}
You can refer codex for more information about post transition.
You're looking for draft_to_publish.
An {old_status}_to_{new_status} action will execute when a post transitions from {old_status} to {new_status}. The action is accompanied by the $post object.

WordPress SEO plugin only visible to specific user

I am using the Yoast SEO plugin in WordPress and wanted to know if there was a way to make it only visible to one specific user in the db or in the functions.php file? Not a role, an actual user.
I tried an universal solution to simply add "plugin-name" and disable it, but failed.
But, to show WPSEO only to a specific user (ID equals 2), the following works:
add_action( 'plugins_loaded', 'seo_so_25654837' );
function seo_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
remove_action( 'plugins_loaded', 'wpseo_admin_init', 15 );
}
Don't add the code to functions.php, use it as a normal plugin.
The following is also needed to remove the SEO menu from the admin bar:
add_action( 'wp_before_admin_bar_render', 'bar_so_25654837' );
function bar_so_25654837()
{
if ( '2' == get_current_user_id() )
return;
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
foreach( $nodes as $node )
{
if( !$node->parent )
{
if( 'wpseo-menu' === $node->id )
$wp_admin_bar->remove_menu( $node->id );
}
}
}
You can hook to pre_current_active_plugins to remove elements from the table before it is displayed. Using get_current_user_id() within the function will let you selectively hide a plugin.
function hide_plugins_by_user( $all_plugins=false ) {
global $wp_list_table;
// if the current user ID is not 1, hide it.
if ( 1 != get_current_user_id() ){
// the active plugins from the table
$plugins = $wp_list_table->items;
// loop through them
foreach ( $plugins as $key => $val ) {
// use the dir + filename of the plugin to hide
if ( $key == 'plugindir/plugin.php' ) {
unset( $wp_list_table->items[$key] );
}
}
}
}
add_action( 'pre_current_active_plugins', 'hide_plugins_by_user' );
This code is working fine (Credits goes to Hislop):
// Returns true if user has specific role
function check_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) )
$user = get_userdata( $user_id );
else
$user = wp_get_current_user();
if ( empty( $user ) )
return false;
return in_array( $role, (array) $user->roles );
}
// Disable WordPress SEO meta box for all roles other than administrator and seo
function wpse_init(){
if( !(check_user_role('seo') || check_user_role('administrator')) ){
// Remove page analysis columns from post lists, also SEO status on post editor
add_filter('wpseo_use_page_analysis', '__return_false');
// Remove Yoast meta boxes
add_action('add_meta_boxes', 'disable_seo_metabox', 100000);
}
}
add_action('init', 'wpse_init');
function disable_seo_metabox(){
remove_meta_box('wpseo_meta', 'post', 'normal');
remove_meta_box('wpseo_meta', 'page', 'normal');
}
Just place it in the functions.php file.
To disable the Yoast for all the users and enable it for just for few or specific, just add the following piece of code to your function.php file.
function remove_wpseo(){
/* if you want to keep it enabled for user with id 2 */
if ( '2' == get_current_user_id() ) {
return;
}
global $wpseo_front;
if(defined($wpseo_front)){
remove_action('wp_head',array($wpseo_front,'head'),1);
}
else {
$wp_thing = WPSEO_Frontend::get_instance();
remove_action('wp_head',array($wp_thing,'head'),1);
}
}
add_action('template_redirect','remove_wpseo');
Reference: https://makersbyte.com/disable-yoast-seo-plugin-specific-page/

Attach media to post WordPress hook

I need an action to be triggered when a detached media is being attached to a post.
It seems that this behavior is handled in wp-admin/upload.php on line 76 by setting the parent of the attachment post with a DB query. There are no hooks in the file.
Is there a way to get an action triggered on this event (some low level DB write hook or whatever) in which I could get the ID of the parent post to which the attachment is being attached?
You could hook into wp_redirect and get the parent post ID from the $_REQUEST. The following is based on an answer I gave here:
add_filter( 'wp_redirect', 'so16798615_wp_redirect', 25, 1 );
function so16798615_wp_redirect( $location )
{
if( ! is_admin() )
return $location;
global $pagenow;
if( 'upload.php' == $pagenow && isset( $_REQUEST['found_post_id'] ) )
{
$parent_id = (int) $_REQUEST['found_post_id'];
if ( ! $parent_id )
return $location;
$parent = get_post( $parent_id );
// do stuff with parent
}
return $location;
}

Resources