WordpPress. Is it possible to implement the following functionality? Essence: The post has a meta-field with values of 1 or 0. It should be displayed as a button so that pressing it switches the value of the field. Convenient change of objects without the need to go to the edit page. If there are other ideas, for example, with radio buttons, a save button, if necessary, suggest. It is important that the update be performed without reloading the page.
- As I understand it, you need something like deluxeblogtips.com/2010/05/add-custom-column.html and hang handlers on AJAX - Heidel
- @Heidel here we are talking about adding a column in the admin panel. This is not what I need. I want to bring the field to archive.php with the ability to change it from there. Anyway, thanks for trying to help. - malginovdesign
- Well, do it by clicking on the button of the adjax-request to the server, and then write a function with the necessary set of actions - alenkins
- @alenkins, I understand, perhaps for you it is - a mere trifle, but I'm not very strong in the scripts (this is putting it mildly). If you help me with this - I will be very grateful to you. - malginovdesign
- you need ajax + update_post_meta - etki
|
2 answers
Common code in single.php for example. In the select, the possible values of the meta-field are displayed, when any value is selected, an agax request is sent to the server that changes the value of the "my_key" field.
<?php $ajax_url = '/wp-admin/admin-ajax.php'; /* url для аджакс-запроса */ $post_id = get_the_ID(); /* id текущего поста */ $nonce = wp_create_nonce('post_id_' . $post_id); /* ключ для проверки */ $my_meta = get_post_meta($post_id, 'my_key', true) ? get_post_meta($post_id, 'my_key', true) : 0; /* текущее значение мета-поля */ ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="//code.jquery.com/jquery-1.12.4.min.js"></script> </head> <body> <select id="my_select"> <?php for ($i = 0; $i < 6; $i++) { ?> <option value="<?php echo $i; ?>" <?php selected($my_meta, $i); ?>><?php echo $i; ?></option> <?php } ?> </select> <div>Текущее значение <span id="my_meta"><?php echo $my_meta; ?></span></div> <script type="text/javascript"> (function($){ $('#my_select').on('change', function(){ var $el = $(this), data = { action: 'ajaxUpdateMeta', /* имя функции-обработчика на сервеной стороне */ meta_val: $el.val(), /* новое значение мета-поля */ post_id: '<?php echo $post_id; ?>', nonce: '<?php echo $nonce; ?>' }; $.post( '<?php echo $ajax_url; ?>', data, function(data){ $('#my_meta').text(data); } ); }); })(jQuery); </script> </body> </html> Handler code in functions.php file:
<?php function ajaxUpdateMeta() { $metaVal = $_POST['meta_val']; $post_id = $_POST['post_id']; $nonce = $_POST['nonce']; $action = 'post_id_' . $post_id; if (wp_verify_nonce($nonce, $action)) { /* проверяем, что ключ верный и обновляем значение мета-поля для переданного id поста */ update_post_meta($post_id, 'my_key', $metaVal); } echo get_post_meta($post_id, 'my_key', true); wp_die(); } add_action('wp_ajax_nopriv_ajaxUpdateMeta', 'ajaxUpdateMeta'); add_action('wp_ajax_ajaxUpdateMeta', 'ajaxUpdateMeta'); Keep in mind that the code is approximate. Validate the values according to the requirements of your task.
- it looks like it should work - malginovdesign
|
index.html file (index.php)
<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <div id='yourId'></div> <button id="buttonId">Click me</button> <script> $("#buttonId").click(function() { num = $("#yourId").text(); $.ajax({ url: "test.php", context: document.body }).done(function(data) { $("#yourId").text(data); }); }); </script> file test.php (explicit update of the field in the database) or use update_post_meta Replace only your requests.
<?php mysql_connect('db_adress', 'mysql_user', 'mysql_password'); $result = mysql_query('your sql to select current num'); $row = mysql_fetch_row($result); if ($row[0] == 0) { $currentNum = 1; } else { $currentNum = 0; } $result = mysql_query('your sql query to update table set $currentNum'); if (!$result) { die('Invalid query: ' . mysql_error()); } echo $currentNum; ?> - Thanks for the answer, I will test your code soon. Can you explain how this script works? There are no standard wordpress functions like
update_post_meta(), so I’m interested. Does he really have to work? - malginovdesign - create a file in notepad and save it with the .html extension and see its work ... and what to explain there all the standard functions .... take any function into Google and read ... wordPress is also cms and not a programming language ... all its methods like update_post_meta () - execute php code that ultimately generates the HTML that you see in the browser, pressing the button on your computer and not on the remote site ... that is, you are essentially working with HTML code ... - Lesiuk Alexey
- I understood you. But your script will not save the value of the field in the database. And I need exactly this. - malginovdesign
- Changed the answer ... - Lesiuk Alexey
- Checked? should work .. - Lesiuk Alexey
|