Calling a function on button click, getting a url Calling a function on button click, getting a url wordpress wordpress

Calling a function on button click, getting a url


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


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