Buddypress edit profile sequential order - php

I have setup Buddypress to have 3 field groups. While the user is on the front end editing their profile, I would like it if when they hit Save that it automatically progresses to the next field group.
So for example I am editing my profile for field group 1 and hit save, it takes me to field group 2 so I can edit that without having to click the nav label for the field.
Because this is a form with method="post" I assumed it would be as easy as checking for a post value. The problem is when I edit the profile and hit save there is not post data. Im using this just for troubleshooting (note these return empty even after I save):
//Next step progression
//Check the field group
echo bp_get_current_profile_group_id();
if(bp_get_current_profile_group_id() == 1) {
echo '<pre>';
print_r($_GET);
echo '</pre>';
echo '<pre>';
print_r($_POST);
echo '</pre>';
if(isset($_POST['_wpnonce'])){ ?>
<script>
jQuery(document).ready(function($){
alert('Saved');
});
</script>
<?php }
} elseif(bp_get_current_profile_group_id() == 2) {
}
After digging a little deeper I see the form action is this: <?php bp_the_profile_group_edit_form_action(); ?> which calls the function <?php bp_get_the_profile_group_edit_form_action(); ?>
I see I can add a filter/action from how this function is documented but I can not seem to get it working.

Try using this hook:
do_action( 'xprofile_updated_profile', bp_displayed_user_id(), $posted_field_ids, $errors, $old_values, $new_values );
Found in buddypress\bp-xprofile\bp-xprofile-screens.php
You'll need to write your own feedback message and check the $posted_field_ids to figure out which group the user was editing.

Thanks to shanebp I was able to find the right hook and get a working solution like this:
in buddypress/members/single/profile/edit.php I added this inside the form tag:
<input type="hidden" name="group_id" id="groupd_id" value="<?php echo bp_get_current_profile_group_id(); ?>" />
Then in my functions I did this which isnt the most dynamic but got me where I needed to go in case anyone else finds this:
function update_xprofile_group_progressions($user_id) {
if (!empty($user_id)) {
if(isset($_POST['group_id']) && $_POST['group_id'] == 1){
$url = home_url() . '/all-members/' . bp_core_get_username($user_id) . '/profile/edit/group/2/';
wp_redirect($url);
exit;
} elseif(isset($_POST['group_id']) && $_POST['group_id'] == 2){
$url = home_url() . '/all-members/' . bp_core_get_username($user_id) . '/profile/edit/group/4/';
wp_redirect($url);
exit;
}
}
}
add_action('xprofile_updated_profile', 'update_xprofile_group_progressions', 0, 1);

Related

Redirect in php not working - WordPress

I am (for the first time) developing a WordPress-plugin which is a simple registration-form. It sends the data to an API. At settings page you can chose to which page the user should be redirected to after the submit is successful. But the redirect is not working.
I am getting the link for the page like this:
$options = get_option('rfw_options');
$successPage = (isset($options['rfw_field_success_page']) ? esc_html($options['rfw_field_success_page']) : 'Select Page');
// some code for API call
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status == 200) { // that means if successful
$successLink = get_permalink(get_page_by_title($successPage));
header("Location: '. $successLink .'");
}
Even if I set the header location like following its not working:
header("Location: https://www.google.no/");
Here you can see how I do set up my form:
function rfw_html_form_code() {
echo '<form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">';
echo '<div class="flex-container"><p>';
_e('Fornavn (obligatorisk)', 'recman-form-widget');
echo '<br><input type="text" name="rfw-fname" pattern="[A-Za-z\wåäöæéøâèêóòôÅÄÖÆÉØÂÈÊÓÒÔ]+" value="' . $_POST['rfw-fname'] . '" required />';
echo '</p>';
echo '<p>';
_e('Etternavn (obligatorisk)', 'recman-form-widget');
echo ' <br /><input type="text" name="rfw-lname" pattern="[A-Za-z\wåäöæéøâèêóòôÅÄÖÆÉØÂÈÊÓÒÔ]+" value="' . $_POST['rfw-lname'] . '" required />';
echo '</p></div>';
}
I hope somebody can help me to solve this issue.
If one of these doesn't work... You're doing something wrong
header( "LOCATION: http://example.com/" ); // Note that this will not change url / state
<head><meta http-equiv="refresh" content="2;url=http://example.com" /></head>
<script type="text/javascript"> window.location = "http://www.example.com/" </script>
You can actually take a reference from this thread How to make a redirect in PHP?
On the other had may be some syntax error. Please try and replace your code with header("Location: ".$successLink."");
Try dumping the variable $successLink with var_dump($successLink);. Inspect if your variable is returing the desired links from the page title or not.
You can also redirect with wp_redirect() function. Take a look around https://developer.wordpress.org/reference/functions/wp_redirect/
WordPress has a inbuilt function for redirection wp_redirect().
You can use it like this:
wp_redirect($successLink);
exit(); //always exit after redirect.
Hope this helps!
Thanks for all the help but I had to work with another solution.
First of all I had to set the action-attribute of the form-element to empty.
<form action="" method="post">
My if-statement looks a little different now (I am just calling a function here now):
if ($status == 200) {
rfw_success_redirect();
$_POST = array();
}
The rfw_success_redirect() function looks like this:
function rfw_success_redirect()
{
$options = get_option('rfw_options');
$successPage = (isset($options['rfw_field_success_page']) ? esc_html($options['rfw_field_success_page']) : 'Select Page');
$successLink = get_permalink(get_page_by_title($successPage));
wp_enqueue_script(
'javascript',
plugins_url('js/javascript.js', __FILE__)
);
$scripts_params = array(
'successLink' => $successLink
);
wp_localize_script('javascript', 'scriptParams', $scripts_params);
}
add_action('rfw_success_redirect', 'my_enqueue_scripts');
In my javascript.js I am setting window.location = scriptParams.successLink;
So I had to use javascript to redirect the page.

PHP - Hide/Show Content based on User Level

I have login page and user page, which after user login with correct username and password, it will go to user page.
The user page consists of a table of user list and have a 'Edit User' button next to each user row. For example, in the screenshot here -->
http://i.stack.imgur.com/K3qxi.png
So, based on user level, if user level = User (which has ID=4), i want to hide the edit button. What I've tried so far is like this but its not working. I have imported the session code and all the queries. This code below is just some part of what I want to do.
include_once("session.php");
if($_SESSION['userlvl']==4){
echo "USER";
?>
<script>
$document.(ready(function){
$(#editbutton).hide();
});
</script>
<?php
}
?>
<a id="editbutton"><img src="Edit.png"/></a>
you can use class instead of Id
include_once("session.php");
if($_SESSION['userlvl']==4){
echo "USER";
?>
<script>
$document.(ready(function){
$(".editbutton").hide();
});
</script>
<?php
}
?>
<a class="editbutton"><img src="Edit.png"/></a>
As was pointed out by #insertusernamehere you should use PHP to control what is written to the page depending upon the user's access level - merely hiding content from view is just asking for trouble as it is trivial to find when looking in code view. Below is just an example of how you might do this.
<?php
include_once("session.php");
?>
<html>
<head>
<title>Restrict content based upon user level example</title>
<?php
if( isset( $_SESSION['userlvl'] ) ){
$level=intval( $_SESSION['userlvl'] );
/*
If you have javascript functions that actually submit the forms or
interact somehow with the data that are to be restricted to particular
levels of user, generate the code for that level only.
*/
switch( $level ){
case 1:
/* Basic user */
echo "
<script id='user' type='text/javascript'>
function view_record(){
alert('hello world');
}
</script>
";
break;
case 2:
/* Superuser */
break;
case 3:
case 4:
/* Manager & Admin level users ? */
echo "
<script id='admin' type='text/javascript'>
function delete_all_users(){
}
function drop_tables(){
}
function drop_database(){
}
function edit_record(e){
alert(e+' edit record')
}
function delete_record(e){
alert(e+' delete record')
}
</script>
";
break;
}
}
?>
</head>
<body>
<table>
<?php
/* Assuming you use php to generate the table: pseudo style code */
while( $rs=$result->fetch() ){
$editbttn='';
$deletebttn='';
if( in_array( $level, array( 3,4 ) ) ){
/* Admin & manager */
$editbttn="<a class='editbutton' onclick='edit_record(event)'><img src='Edit.png'/></a>";
$deletebttn="<a class='deletebutton' onclick='delete_record(event)'><img src='Delete.png'/></a>";
}
echo "
<tr>
<td>DATA</td>
<td>DATA</td>
<td>DATA</td>
<td>$editbttn</td>
<td>$deletebttn</td>
</tr>";
}
?>
</table>
</body>
</html>
I assumed that you want to restrict the user that only has the privilege of "User" not to be able to edit the data?
You might want to consider trying this as I think it is more straight forward and simple?
session_start();
if($_SESSION['userlvl'] == 4) {
echo "USER";
}else{
$userlvlpermission = '<a id="editbutton"><img src="Edit.png"></a>';
}
//At where you wanted to place the "editbutton"
<?php if($userlvlpermission){echo $userlvlpermission;}?>
Be sure that you check the user session of the edit page as well in case they know the exact url.
AT edit page
<?php
session_start();
if ($_SESSION['userlvl'] = 4){
echo "NO PERMISSION";
echo "header('Refresh: 3;url=page.php')";
exit();
}
Here's a simple solution.
include_once("session.php");
if ($_SESSION['userlvl'] == 4) {
$hideme = 'display:none;'
} else {
$hideme = '';
} ?>
< a id = "editbutton"
style = "<?php echo $hideme; ?>" > < img src = "Edit.png" / > < /a>

How to condition a user to fill the form than to oppen an specific site up

i have a multi step form and want to condition users on specific sites on my web .
This mean i want that only after submitting my form a client in my case can see the redirected page ,
And that with a kinda tim-out for that page to . this redirected page need to show only to those people who fill the form first even when users copy the link and give that link to somebody else the link should not work or should direction in a error. i have archived the last part partly
Here is all my code :
On the form.php i have this :
<?php
session_start(); $_SESSION['form_finished'] = true;
?>
On the proces.php i have this :
$emotion = $_POST['emotion'];
if($emotion == 'Basic Pack') {
session_start();
$_SESSION['form_finished'] = true;
header('Location: /new/basicc.php');
} elseif($emotion == 'Deluxe Pack') {
header('Location: html6.php');
} elseif($emotion == 'Premium Pack') {
header('Location: html7.php');
}
and destination site in this case basicc.php' this :
<?php
session_start();
if(!$_SESSION['form_finished']) {
header("HTTP/1.0 404 Not Found");
exit;
}
?>
This code is working partly because if the user on the form.php site if he just copy the basicc.php link on the address bar he can see the basic.php site imadtitly without having to fill the form , and i want that to condition him to do that and than the page to show up .
I hope i was clear thanks in advance
If proces.php is where submitting the form redirects then remove $_SESSION['form_finished'] = true; from form.php and keep it in proces.php only.
ETA: For the timer:
<script>
var remainingSeconds = 600; // how many second before redirect
function counter() {
if (remainingSeconds == 0) { clearInterval(countdownTimer); window.open('form.php', '_SELF'); // return to form page
} else { remainingSeconds--; }
}
var countdownTimer = setInterval('counter()', 1000); // 1000 is the interval for counting down, in this case 1 second
</script>
In this case, you will have to add back the statement in form.php but set it to false $_SESSION['form_finished'] = false;
ETA2: Forgot to mention that you should also add $_SESSION['form_finished'] = false; in basicc.php.
Yes you could just use a simple session for this case. Example:
If in your form action, if the form processing is in process.php. You could initialize there the session.
session_start();
$emotion = $_POST['emotion'];
$_SESSION['form_finished'] = true; // set session
// then your other process etc. etc.
if($emotion == 'Basic Pack') {
header('Location: /new/basicc.php');
} elseif($emotion == 'Deluxe Pack') {
header('Location: html6.php');
} elseif($emotion == 'Premium Pack') {
header('Location: html7.php');
}
And then on the destination files: /new/basicc.php and others, check that session existence:
/new/basicc.php and others:
if(isset($_SESSION['form_finished'])) { // so after redirection check this
//
// hello, i came from process.php
unset($_SESSION['form_finished']); // and then UNSET it! this is important
} else {
echo 'not allowed'; // if this is not set, the page is directly accessed, not allowed
exit;
}
I think the best solution is that you should only use one page, no need for sessions ;)
Try to have a particular variable set to false, send your form to the server using a POST method <form method=post> and on your server, change this variable to true and render the same page again.
In the example below, I'm checking if the user has entered his name in the form. ;)
<!-- In form.php -->
<?php
$formSubmitted = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["name"])) {
//Do what you need to do with form data, for example:
$name = filter_var($_POST["name"],FILTER_SANITIZE_STRING);
//Maybe do some checks on the data (or add to database) and when successful:
if($name != '')
{
$formSubmitted = true; // Set variable to true
}
}
?>
<?php if($formSubmitted): ?>
Hello <?php echo $name; ?>! <!-- Show all other HTML things you want to show -->
<p> This is a message only for people who submitted the form! </p>
<?php else: ?>
<form action='form.php' method='POST'>
<input name='name' type='text'>
</form>
<?php endif; ?>
I hope it'll be useful and hopefully a different way to look at the problem. For multi-step, this could easily accommodate more variables to see which step the user is on ;)
Good luck :)

Remove item from session (show a remove button)

I have a button on a page, It's a simple input submit button, which sends the page ID to a form on another page to save it in a PHP session.
I was wondering how I'd go about showing a remove button instead if that item is already in your session?
This is the code that the form gets sent to
<?php
session_start();
if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
The page with the button doesn't actually have any session code, but I guess it would be a similar IF kind of statement, but I am unsure of the exact syntax I should use? And how would it take the ID away from session?
EDIT:
Would something like this work? To put around the form button.
<? if(isset($_SESSION['event_orders']))
{
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
// echo the submit button;
} ?>
You can use unset function. Reference: http://php.net/manual/en/function.unset.php
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders']);
}
else if(isset($_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
echo "<a href='".$_SERVER['PHP_SELF']."?delete'>delete</a>";
}
else if(!in_array($_POST['event_id'], $_SESSION['event_orders'])) {
$_SESSION['event_orders'][] = $_POST['event_id'];
}
?>
EDIT:
we need only to delete the specific array items instead of fully unseting the array.
<?php
session_start();
//if request parameter delete is present then unset the array
if(isset($_REQUEST['delete']))
{
unset($_SESSION['event_orders'][$_REQUEST['delete']]);
var_dump($_SESSION['event_orders']);
}
else if(in_array($_POST['event_id'], $_SESSION['event_orders']))
{
//displays delete link which redirects to same page with an additional request parameter as url?delete
$key = array_search($_POST['event_id'], $_SESSION['event_orders']);
var_dump($_SESSION['event_orders']);
echo "<a href='".$_SERVER['PHP_SELF']."?delete=".$key."'>delete</a>";
}
else
{
$_SESSION['event_orders'][] = $_POST['event_id'];
var_dump($_SESSION['event_orders']);
}
?>

passing values through the URL when using the $_GET just get blank page

this is the code used to get the information from the database, the images I have taken off the echo statement for the time being, and just the name of the product. When I click on the product name it sends me to cart.php and should pass the value in the URL it shows in the browser when I hover over the text but when i click it send me to cart.php and just shows a blank page
$product_types = get_all_subjects2(); function is just the query
while($products = mysql_fetch_array($product_types))
{
$name = $products['name'];
$address = $products['image_location'];
echo '<ul>';
echo "<li><a href=\"http://localhost/project/cart.php?subj=" . urlencode($products["name"]) .
"\">{$products["name"]}</a></li>";
The code
<?php
if (isset($_POST['subj']))
{
$a = $_POST['subj'];
echo $a;
}
else {
echo"error";
}
?>
$_POST stored values following POST request. If you navigate via a plain link, you're doing GET requests. So look in $_GET.
Also it might be a good idea to always output valid HTML. Otherwise the browser might or might not render what you're outputing.
You send your arguments via GET. So asking for $_POST isn't the right idea ;).
For Debbugging it's nice to have this statement in your target site:
echo '<pre>', print_r($_REQUEST), '</pre>';
Best,
Christian
Addition: tested the code.. just works fine:
<?php
echo '<pre>', print_r($_REQUEST), '</pre>';
if (isset($_GET['subj'])) {
$a = $_GET['subj'];
echo $a;
} else {
echo 'error';
}
?>

Categories