Redirect after front end deleting post - php

I've created a delete post button on the front end for a Wordpress site. It deletes the post fine but then tries to reload the post again causing a 404. Is there a way I redirect it after deleting to a specific url? This is my code:
function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
global $post;
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}
Then on the template:
<?php wp_delete_post_link('Delete This', '<p>', '</p>'); ?>

I think you are looking for the wp_redirect function.
$location = 'http://domainname.com/pagename/';
$status = '301';
wp_redirect( $location, $status );
exit;
Just place after your successful delete code.

How about running this function right after your echo:
header( 'Location: yournewURL.html' ) ;
Replace 'yournewURL.html' with the page you need to redirect to.

Related

php variable using mongodb not showing on website server

i am making a twitter clone website using php, and this is the script for creating a tweet:
<?php
session_start();
require_once('twitterdbconnect.php');
if(!isset($_POST['body'])) { //see if the post variable has the body of our tweet
exit;
}
$user_id = $_SESSION['user']; //if there is a tweet get the users id from session
$userData = $db->users->findOne( array('_id'=>$user_id)); //get the users data by asking the database for this user
$body = substr($_POST['body'], 140); //body is the first 140 characters of the post
$date = date('Y-m-d H:i:s');
$db->tweets->insertOne( array( //create a tweets collection and send the tweet there
'authorId' => $user_id,
'authorName' => $userData['username'],
'body' => $body,
'created' => $date
));
header('Location: home.php');
?>
this is the script of printing it on the home page:
<div>
<?php
$recent_tweets = get_recent_tweets($db);
foreach($recent_tweets as $tweet) {
echo '<p>' . $tweet['authorName'] .'</p>';
echo '<p>' . $tweet['body'] . '</p>';
echo '<p>' . $tweet['created'] . '</p>';
echo '<hr>';
}
?>
</div>
the other variables seem to work, but the body variable does not show up and the end result is so:
How can I make it appear as so:
?

Use an external Link as the WordPress profile picture

I want to write a Single Sign On (SSO) plugin for WordPress. I want to use an external image link as user's profile picture after user login.
This image has a link which is located in http://www.example.com/image.png.
How can I do this in WordPress?
Please add this code to your activated theme's functions.php file.
add_filter( 'author_link', 'modify_author_link', 10, 1 );
function modify_author_link( $link ) {
$link = 'http://google.com/';
return $link;
}
You can use your any link, just change https://google.com to your link.
Thanks.
These Hooks ('get_avatar', 'avatar_defaults') are also available by WordPress, as an example:
you can check the documentation here:
https://codex.wordpress.org/Function_Reference/get_avatar
function my_custom_avatar($avatar, $id_or_email, $size, $default, $alt)
{
echo $avatar . ' -> ' . $id_or_email . ' -> ' . $size . ' -> ' . $default . ' -> ' .$alt;
$avatar = 'https://www.example.com/yourImage';
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}
add_filter( 'get_avatar', 'my_custom_avatar', 10, 5 );
add_filter( 'avatar_defaults', 'my_custom_avatar', 10, 1 );```

How to handle user_row_actions in WordPress?

First of all, I have not find solution for my problem. I have read few articles and thread to create a user actions , and tried the following code.
function kv_admin_deactivate_link($actions, $user_object) {
$actions['deactivate_user'] = "<a href='" . admin_url( "users.php?action=deactivate&user=$user_object->ID") . "'>" . __( 'Deactivate', 'kvc' ) . "</a>";
return $actions;
}
add_filter('user_row_actions', 'kv_admin_deactivate_link', 10, 2);
After using the above code it gets me the additional action with users list as like in the below screenshot.
Now, my problem is, I don't know how to proceed to write my codes to deactivate a user. So can you help me to write function to handle /wp-admin/users.php?action=deactivate&user=41. Here I am writing these function for my wordpress theme and how can I write function for it.
This will help you to perform your deactivate operation function. just create a admin menu with the following code.
function xxxadmin_submenu_email() {
add_menu_page('Your menu', 'Your menu', 'manage_options', 'deactivate' , 'xxx_deactivate_functions', '', 66);
}
add_action('admin_menu', 'xxxadmin_submenu_email');
Now, it will get you a page and you have to write function inside xxx_deactivate_functions it. Here is sample code.
function xxx_deactivate_functions() {
if(isset($_GET['action']) && $_GET['action']== 'deactivate'){
$user_id = $_GET['user'];
$user_info = get_userdata($user_id);
}
}
And I have modified your function like the below one.
function kv_admin_deactivate_link($actions, $user_object) {
$actions['deactivate_user'] = "<a href='" . admin_url( "users.php?page=deactivate&action=deactivate&user=$user_object->ID") . "'>" . __( 'Deactivate', 'kvc' ) . "</a>";
return $actions;
}
add_filter('user_row_actions', 'kv_admin_deactivate_link', 10, 2);
This is one way to do this. But wait for some other experts solution to code it better way. Hope its helpful for urgent need.

buddy press profile link not going to correct place

I created a bp-custom.php and regrouped the menu items fine. But now i am trying to add a link to go to /site/members. It list all the members.
When i add it though it goes under the profile I am viewing. I am redirecting to a wordpress page if that helps. Or is there a better way to do this.
Ex :
http://website.com/log-in/members/username/members/
I want it to go just here
http://website.com/log-in/members/
I would love to learn how to just put a url and no slug but whatever works. I do not know why it keeps referencing that signed in /member/username. I have even tried parent url and that did not work. I might have been using parent url syntax wrong.
Here is the function
function mb_bp_profile_menu_posts() {
global $bp;
bp_core_new_nav_item(
array(
'name' => 'Members',
'slug' => 'members',
'position' => 60,
)
);
}
I know that i can create .htaccess for this. But I don't want to do it.
May i know what is the clean way (alternate way) to do this?
I have tried what the user said in comment below and found in bp-members-template this function. I then added the part in bold to add the link but that did not work. I am just adding a google link for testing only.
function bp_get_displayed_user_nav() {
global $bp;
foreach ( (array) $bp->bp_nav as $user_nav_item ) {
if ( empty( $user_nav_item['show_for_displayed_user'] ) && !bp_is_my_profile() )
continue;
$selected = '';
if ( bp_is_current_component( $user_nav_item['slug'] ) ) {
$selected = ' class="current selected"';
}
if ( bp_loggedin_user_domain() ) {
$link = str_replace( bp_loggedin_user_domain(), bp_displayed_user_domain(), $user_nav_item['link'] );
} else {
$link = trailingslashit( bp_displayed_user_domain() . $user_nav_item['link'] );
}
echo apply_filters_ref_array( 'bp_get_displayed_user_nav_' . $user_nav_item['css_id'], array( '<li id="' . $user_nav_item['css_id'] . '-personal-li" ' . $selected . '><a id="user-' . $user_nav_item['css_id'] . '" href="' . $link . '">' . $user_nav_item['name'] . '</a></li>', &$user_nav_item ) );
**echo "<a href='http://www.google.com'>Google</a>"; }**
}
The bp_core_new_nav_item function is used to add a link to the user's navigation which explains why you're seeing URLs like /members/username/members/ when clicking on the tab. I don't think bp_core_new_nav_item is the right approach here.
An alternative approach would be to replace the function in your theme template that outputs the navigation with your own custom menu.
See this article on the BP Template Hierarchy which shows you how you can set up your own templates:
http://codex.buddypress.org/themes/theme-compatibility-1-7/template-hierarchy/

How to insert a php code from another php file?

Im creating a custom function for my wordpress website that will add a review section below the post content and i need to insert this function from another another file into a custom function that i added to my functions.php. I need to get this piece of code $buffy .= $this->get_review(); from a different file to work in this function:
function content_injector($content) {
global $wp_query;
$postid = $wp_query->post->ID;
if (is_single((get_post_meta($postid, 'top_ad', true) != 'off' ))) {
$top_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
}
if (is_single((get_post_meta($postid, 'bottom_ad', true) != 'off' ))) {
$bottom_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
}
if (is_single()) {
$review = //HOW DO I ADD THAT get_review CODE HERE?
$custom_share = '<div id="title-deel"><h4 class="block-title"><span>DEEL</span></h4></div>' . do_shortcode('[ssba]');
$facebook_comments = '<div id="title-reageer"><h4 class="block-title"><span>REAGEER</span></h4></div>' . '<div class="fb-comments" data-href="' . get_permalink() . '" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';
}
$content = $top_ad . $content . $bottom_ad . $custom_share . $facebook_comments;
return $content;
}
add_filter('the_content', 'content_injector');
As you can see i need to add the get_review function to $review, but i cant make it work on my own. How to make this work?
Use include to include the file before using any methods from that file.
include("file_with_functions.php");
OR
Create a class (with filename same as classname).
Include the file.
Create an instance of the class.
Access the method in the class through the object

Categories