Calling a function on button click, getting a url

1k views Asked by At

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">
        ';
}
2

There are 2 answers

5
Akshay Shah On

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">
    ';    
 }
}
0
Kodaloid On

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