Calling a function on button click, getting a url
Asked Answered
A

2

9

I am new to wordpress. I am trying to call function myprefix_edit_user_cb() to get the edit form after user clicks on edit.

function getdata()
{    
    $blogusers = get_users();
    foreach ( $blogusers as $user ) {
        echo '<span>' . esc_html( $user->user_email ) . '</span>';


        $editUrl = ??

        echo  "<a href='".$editUrl. "'>Edit User</a>"; 

        echo '<br>';
    }   
}

with function:

 function myprefix_edit_user_cb(){    
     $user = intval($_REQUEST['user']);

    echo '
                <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
                <label>Username</label>
                <input type="text" value="' .$user->user_login  . '"
                <input type="submit">
        ';
}
Aptitude answered 1/9, 2017 at 3:58 Comment(5)
Where is this hook defined: myprefix_edit_user ?Wally
@MihaiPapuc how should I? i am new and don't how.Aptitude
Where did you find the documentation for this hook?Galvez
@Steve, your code above is out of context, so there is nothing much we could help you with. How about rephrasing the question to point out exactly what you need to do (eg. what edit form), what are your ideas to solve your problem and what are the sources that you're basing your ideas on. I think we (or other users) could help more with this kind of information.Wally
I ama not getting an overall idea of the code you have given, but my suggestion is check whether your theme contain any do_action('myprefix_edit_user') fn before clicking the button.Solution
M
3

According to me you need to put some request flag with your edit url.

Try the below code.

function getdata(){    
$blogusers = get_users();
foreach ( $blogusers as $user ) {
    echo '<span>' . esc_html( $user->user_email ) . '</span>';

    $deleteUrl = add_query_arg(array('action'=>'myprefix_delete_user', 'user_id'=>$user->ID));

    $editUrl = add_query_arg(array('action'=>'myprefix_edit_user', 'user'=>$user));

    echo  "<a href='".$deleteUrl. "'>Delete User</a>"; 
    echo  "<a href='".$editUrl. "&edit=1'>Edit User</a>"; 

    echo '<br>';
 }   
}

with action and callback function with flag :

add_action('init','myprefix_edit_user_cb');
function myprefix_edit_user_cb(){    
 $user = intval($_REQUEST['user']);
 if($user == '')
 return;

 if($_REQUEST['edit'] == 1 )
 {
    echo '
            <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
            <label>Username</label>
            <input type="text" value="' .$user->user_login  . '"
            <input type="submit">
    ';    
 }
}
Make answered 5/9, 2017 at 6:19 Comment(5)
the function myprefix_edit_user_cb() is never running to reach there. I've edited the question.Aptitude
after my code i guess your issues must be resolved. have you check that?Make
Please check nowMake
@Steve, Please check the answer nowMake
It looks as though you are looking for the ->user_login property on an int?? There's a $user = get_user_by( 'id', $user ); missing before your form.Oscar
S
3

What you are asking all depends on where you would like to allow the user to be edited. Here is my preferred option (assuming you are doing everything on the front side of the website):

Create a page with a page template.

By default most themes come with some basic templates for how a page will look. Seeing as you may wish to add an edit form to a page, creating a custom page template would be a straight forward move. A good tutorial for creating these can be found here. Once created you would add some code like this to the template:

<?php if (isset($_GET['user_id'])): ?>

    <?php $user = get_user_by('id', intval($_GET['user_id'])); ?>
    <form action="#" method="post">
        <label>Username</label>
        <input type="text" value="<?= esc_attr($selected_user->user_login); ?>" />
        <input type="submit" />
        ...
    </form>

<?php else: ?>

    <p>Error, please specify a user id!</p>

<?php endif; ?>

Which would do a basic test to make sure user_id had been passed to the page, then load the form accordingly (to improve on this I would also check to see if get_user_by returns an object before showing an edit form just in-case the user_id is invalid). In the provided example a URL (with permalinks set to page-name) would look like this:

https://example.com/edit-page/?user_id=55

There are ways of making the URL cleaner, however for now I am just trying to make sure your question is answered with a correct working example.

Koda

Segal answered 10/9, 2017 at 3:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.