Calling a function on button click, getting a url - php

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

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

Related

Fill input with array

In my jquery code I have:
$("#show").append("<img src=" +attachment.url+" alt="+attachment.alt+" title="+attachment.title+" description="+attachment.caption+" class='img-responsive img-thumbnail'/><input type='hidden' name='my_image_URL[]' value="+attachment.url+"></span>");
My jquery code adds fields for news selected images and fills inputs names my_image_URL[].
In PHP:
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
}
I trying to add $urls array in the hidden input.
And after, if its works ok:
<?php
if ($urls != '' ) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<input name="my_image_URL[]" value="<?php echo $url;?>"/>
</div>
<?php
};
}
?>
But this part of code not fill the input:
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
}
---------- UPDATE --------
options.php
register_setting(
'tema-setting-group',//string $option_group
'imagens_home' //string $option_name
//calback $sanitize_calback
);
---
add_settings_field(
'home-imagens-top',//string $id
'Imagens',//String $title
'tema_home_imgs',//string $calback
'opcoes_do_tema',//string $page
'tema-home-options'//string $section
//string $args
);
//calback
function tema_home_imgs(){
$urlsImagens = esc_attr( get_option( 'imagens_home' ) ); // RETURN DB DATA
include( get_template_directory() . '/inc/templates/selecao_imagens.php');
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input name="imagens_home" value="'.$json_encode($urls).'" style="width:300px"/>';
}
}
selecao_imagens.php
<input id="my_upl_button" type="button" value="Escolher Imagens" /><br/>
<div class="row">
<div id="exibe" class="sortable">
<?php
$urls = json_decode($urlsImagens, true);
if ($urls != '' ) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<input name="my_image_URL[]" value="<?php echo $url;?>"/>
<?php
};
}
?>
</div>
</div>
theme_options.php
<?php settings_errors();?>
<form method="post" action="options.php">
<?php settings_fields ('tema-setting-group'); ?>
<?php do_settings_sections (
'opcoes_do_tema'//string $page
); ?>
<?php submit_button ();
?>
</form>
-------- UPDATE 2 -------
I tried:
echo '<input name="imagens_home" value="' . htmlspecialchars(json_encode($urls)) . '" />';
but it is not yet filling in the input.
I also tried only:
If (isset ($ _POST ['my_image_URL'])) {
Print_r ($ _ POST ['my_image_URL']);
}
But after the submit does not appear anything on the screen, in the form correctly saves all other inputs except what I am trying to save the array, if I put some manual information goes ok. But I do not understand why it is not capturing the my_image_URL [] names of each image input. The action in form is like this:
<Form method = “post” action = “options.php”>
I’m using the Settings API
Thanks
I trying to add $urls array in the hidden input.
If I understand, you're trying to store an array value in a hidden input, so you can retrieve it later. Problem is, this doesn't work...
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
...because when you echo a PHP array all you get is the string Array, not the actual array contents.
You could turn the array into a json string though:
echo '<input type="hidden" name="imagens_home" value="'.$json_encode($urls).'"/>';
Now your hidden input has a regular string. Later, when the form is POSTed, you could retrieve it:
$urls = json_decode($_POST['imagens_home'], true)

PHP MVC not sending value to controller

I have been making a MVC site, I am having trouble sending the row id from my form to my controller.
The code that I am using for my form gets the row ID for each db entry and assigns it to a hidden value. When the form is submitted it sends the parameters to the controller (should send $uid) but the uid isn't making it to the controller.
Form Code (buttons.php)
<?php
$itemsDAO = new ItemsDAO();
$result = $itemsDAO->getItems();
foreach ($result as $row) {
$uid = $row['id'];
?>
<form action="index.php" method="post">
<fieldset>
<input id='action' type='hidden' name='action' value='deleteItem' />
<p>
<div class="form-group">
<div class="controls">
<input type="hidden" id="fId" name="fId" value="<?php echo $uid; ?>">
<input type="submit" class="btn btn-success" value="Delete">
</div>
</div>
</p>
</fieldset>
</form>
<?php } ?>
Controller function
function deleteItem($parameters) {
$id=$parameters["fId"];
if ($this->model->deleteItem( $id )) {
$this->model->hasDeleteFailed = false;
$this->model->setDeleteItemConfirmation();
return (true);
}
else
$this->model->deleteItemError ( DELETE_ITEM_ERROR_STR );
}
View.php - where I am showing the list of db items and the buttons.php
$this->model->prepareItemList();
$buttons = file_get_contents( "templates/buttons.php");
$HTMLItemList = "";
foreach ( $this->model->itemList as $row )
$HTMLItemList .= "<li><strong>" . $row ["title"] . ": </strong>" . $row ["price"] . "<blockquote>" .$row ["description"] . " " . $buttons ."</blockquote></li>";
Try $_POST["fld"]; in your controller to get value. If you are using any framework like codeigniter then you can use its own methods.
For example codeigniter has
$this->input->post();
Okay,
step by step the $uid has to first make it into the form elements value attribute. Check the html source code to make sure this is actually happening.
place var_dump($_POST) exit; in your controller to find what is actually being recieved if anything at all.
check to make sure your result array element actual has a value and not an empty string value or NULL.
Hmm S.O. code formatting bad.
// turn on error reporting for dev to view empty or missing variable errors
ini_set('error_reporting', E_ALL);
$result=$itemsDAO->getItems();
foreach ($result as $row) {
($row['id'] != ''? $uid = $row['id'] : $uid ='no id found');
// debug result
echo '<pre>' . print_r($row,1) .'</pre>';
}

$_POST and code manipulation

I want to grab the value of a field using $_POST, manipulate it, then pass the value back to the same page to the same field before the PHP code manipulates it.
If I put the PHP code after the field, it manipulates the code, reloads the page but doesn't put the manipulated code back into the field.
if (!isset($input)) {
$input = '';
}
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
If I put the PHP code before the field, it can't find the field to manipulate the value...
if (!isset($input)) {
$input = '';
}
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
Obviously the first approach is more correct, but how do I pass the $input variable to the field before the rest of my PHP manipulation code executes?
I tried $_POST['inputText'] = $input as a desperate attempt but nothing..
Well, from what I've understood in your explanation, you want to change the input value to something else and show it in he same field. If that's correct, you may want to do this:
<form id="testform" method="post" action="">
<input type="text" name="inputText" value="<?php echo ( isset($_POST['inputText']) ) ? sprintf( '%s manipulated', $_POST['inputText'] ) : ''; ?>">
<button type="submit"> Send </button>
</form>
Let me know if that's what you wanted. Regards !
Try
$input = isset($_POST['inputText']) ?$_POST['inputText'] :'';
in the begining instead of
if (!isset($input)) {
$input = '';
}

WordPress : Password Protected Page (2)

I just upgraded my site to a new version WordPress 3.9.2. I noticed that one of my page is not working the way it usually does. This page is password protected and I made changes on how it looks. When I upgraded, it doesn't work anymore. In the password protected page, I have this code:
<?php
echo "<script type='text/javascript'>\nwindow.location = 'http://www.google.com'</script>";
?>
The purpose of that one is to redirect to another page. And they go hand in hand with this code below.
Here is my old code:
<?php
function my_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form action="' . get_option('siteurl') . '/wp-pass.php" method="post">
' . __( "To view this protected post, enter the password below:" ) . '
<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
</form>
';
return $o;
}
add_filter( 'the_password_form', 'my_password_form' );
?>
Before the upgrade, after inputting the password it redirects me to another page, which is how I wanted it to work. But take note of the action attribute of form. In the WP 3.9.2, wp-pass.php does not exist anymore so I was looking for another code. I saw this line:
action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '"
But after inputting the password, it redirects me to the wp-login which is not what I wanted. I need help with this, which works the same way with the old code I'm using. I am not going to downgrade my WP or install any plugin. I just want the value of the action="" changed. Thanks!
I already found the answer. Maybe my files weren't compatible that's why it didn't work but here is the full code.
<?php
function my_password_form() {
global $post;
$label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
$o = '<form action="' . get_option('siteurl') . '/wp-login.php?action=postpass" method="post">
' . __( "To view this protected post, enter the password below:" ) . '
<label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" maxlength="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
</form>
';
return $o;
}
add_filter( 'the_password_form', 'my_password_form' );
?>
NOTE : I am using WordPress 3.9.2
I had the same problem and i found a solution
1) Set your page private with a password
2) Insert this form in an other page (typique postpass wordpress form) :
<form action="https://exemple.com/wp-login.php?action=postpass" class="post-password-form" method="post" id="go-pro-espace">
<input name="post_password" id="exemple" type="password" size="20" /><br />
<input type="submit" value="submit">
</form>
3) Change your /wp-login.php file (root directory, at this time, this is located line 460) :
from:
wp_safe_redirect( wp_get_referer() );
to:
wp_safe_redirect( "https://exemple.fr/your-protected-page" );
Take a look to the answer: Wordpress protected page, POST form on a other page

My Email To Tumblr

This is the email I just sent to Tumblr for help on this issue. If any of you know anything about this, I would GREATLY GREATLY appreciate it!
For awhile now I've been trying to display my blog on my website, but I can not seem to find anyway to do it. I mean I've tried everything. From the JS, and incrementing it through PHP.
<?php
$location = isset( $_REQUEST['nav'] ) ? $_REQUEST['nav'] : '';
$page = isset( $_POST['page'] ) ? $_POST['page'] : '0';
if (isset($_POST['next'])) {
$page++; }
else if (isset($_POST['previous'])) {
$page--;}
if ($page === 0) {
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/js'></script>"; }
if ($page === 1) {
$page++;
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/page/" . $page . "/js'></script>"; }
else {
echo "<script src='http://myblog.tumblr.com/tagged/" . $_REQUEST["tag"] . "/page/" . $page . "/js'></script>"; }
?>
<form method="POST">
<div class="btn-group">
<input type="submit" name="next" class="btn btn-large" value="Click For Next Page" />
</div>
<input type="hidden" name="navigation" value="location" />
<input type="hidden" name="page" value="<?php echo $page ?>" />
<input type="hidden" name="navigation" value="<?php echo $location; ?>" />
</form>
But you only really do that if you have a "tag", and I want to display all of my posts. So I even tried your JSON.
<script type='text/javascript' src='http://myblog.tumblr.com/api/read/json'></script>
<?php
$offset = 9;
$page = 1;
$placeholder = 1;
if (isset($_GET['post']) && is_numeric($_GET['post'])) {
$page = $_GET['post'];}
$start_number = ($page - 1) * $offset;
$end_number = $start_number + 9;
$num = $start_number;
while ($num <= $end_number) {
echo "<img border='0' style='margin-bottom:15px;' id='ji-tumblr-photo-myblog-" . $placeholder . "' src='' alt='' />\n";
echo "<script type='text/javascript'> document.getElementById('ji-tumblr-photo-myblog-" . $placeholder . "').setAttribute('src', tumblr_api_read.posts[" . $num . "]['photo-url-500']);</script><br />\n";
$num++;
$placeholder++;}
echo sprintf('More', $page + 1);
?>
But with the JSON you can only display the last few posts, and can't access all of them! So you can see my struggle, and all I want is a widget for my website. I post all of my photography, videos, life on Tumblr. I love Tumblr! PLEASE PLEASE PLEASE, help me in anyway possible.
Cheers,
Jade Allen Cook
You could run your Tumblr under your own (sub) domain.
You could copy all your posts to your own database. You could use your Tumblr’s sitemap to get all post URLs: http://MYBLOG.tumblr.com/sitemap.xml. Then you could scrape/download your posts.
You could run a tumblelog on your own site and duplicate all your posts to Tumblr via their API.
The last way should be preferred. That way it’s your content. Tumblr might will be gone sometime and with it all your content/URLs. But if you use your own site/server as base, you will have no problem if Tumblr goes evil. You can also easily move your content to the next hot centralized service that will appear.

Categories