I'm using the latest version of Wordpress and I want to upload a small plugin - a metabox that lets editors add source links in their articles. The code looks like this:   /* Do something with the data entered */ add_action( 'save_post', 'wpse_source_link_save' );   /* Adds a box to the main column on the Post and Page edit screens */ function wpse_source_link() {       add_meta_box(         'source_link',         __( 'Source-link', 'myplugin_textdomain' ),          'wpse_source_meta_box',         'post',         'side'     ); }   /* Prints the box content */ function wpse_source_meta_box( $post ) {     // Use nonce for verification   wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );     // The actual fields for data entry   echo '&lt;label for=&quot;source-link&quot;&gt;URL:&lt;/label&gt; ';   echo '&lt;input type=&quot;text&quot; id=&quot;source-link&quot;&quot; name=&quot;source_link&quot; value=&quot;'.     get_post_meta( $post-&gt;ID, '_source_link', true ) .'&quot; size=&quot;25&quot; /&gt;';     echo '&lt;label for=&quot;source-link-label&quot;&gt;Label:&lt;/label&gt; ';   echo '&lt;input type=&quot;text&quot; id=&quot;source-link-label&quot;&quot; name=&quot;source_link_label&quot; value=&quot;'.     get_post_meta( $post-&gt;ID, '_source_link_label', true ) .'&quot; size=&quot;25&quot; /&gt;'; }   /* When the post is saved, saves our custom data */ function wpse_source_link_save( $post_id ) {   // verify if this is an auto save routine.    // If it is our form has not been submitted, so we dont want to do anything   if ( defined( 'DOING_AUTOSAVE' ) &amp;&amp; DOING_AUTOSAVE )        return;     // verify this came from the our screen and with proper authorization,   // because save_post can be triggered at other times     if ( ! wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )       return;       // Check permissions     if ( current_user_can( 'edit_post', $post_id ) ) {         update_post_meta( $post_id, '_source_link', sanitize_text_field( $_POST['source_link'] ) );       update_post_meta( $post_id, '_source_link_label', sanitize_text_field( $_POST['source_link_label'] ) );      } }   Metabox works fine and It's possible to add links along with anchors. The problem is, it doesn't show up in posts. I can't figure out what is wrong with this code. Does anyone have any ideas?
121447
I'm using a little plugin that allows me to add source link to every article. I found the code in this thread:   It's almost perfect, but I need to make 2 little modifications:   When there's no source link, the word "Source:" is still showing up (here's how it looks like: ), but I need to get rid of it if no source link is added. Does anyone have any idea how to do it? Source link shows on every page of a post. I have some posts splitted into multiple pages, but the link still shows on each one of them. I need to change this - the link has to show up only on the last page of a splitted post. Any ideas how to achieve that?   Here's the code I'm using (pasted in functions.php):  - the first one  And here's what I have in my single.php (single post):   &lt;div class="source-link"&gt;Source: &lt;?php echo esc_url( get_post_meta( $post-&gt;ID, '_source_link', true ) ); ?&gt;&lt;/div&gt;   Thanks in advance. Im desperate for help.  Kacper
97826
I'm currently writing a plugin that requires the PayPal IPN, since the IPN is not inside the main plugin file, I have no way to use $wpdb. If you know a way to do this that will work with all WordPress installations it would really help me!  Thanks in advance!
94001
For starters I am using this GREAT answer to achieve custom post types as well as a custom way to insert those post types into other pages, you can view that answer here:   I am running into a problem however when wordpress auto saves itself, it clears whatever metadata i have put into the forms.  EXAMPLE:  I select the 3 drop downs I want from my metabox and hit update.  The page reloads and the 3 choices are still selected.  In phpMyAdmin i can look up the metadata and see the array in place as it should be.  I now wait for wordpress to auto save.  As soon as this happens i can either refresh the page (not update, just refresh) and the metadata is lost, or i can look up the metadata in phpMyAdmin and it will display nothing (ie: its gone).  I'm guessing that this has something to do with needing to tie into another hook (some sorta auto update hook) or something, but even then it doesn't make sense that it would actually delete your metadata (rather than just not auto updating it).  Last note: If you copy and paste Mikes code into functions.php, you should be able to recreate my error (in wordpress 3.1) very easy.
61666
In reading I am not sure if the issue below could have something to do with needing a custom wordpress ajax handler? All of these functions are inside my functions.php file. I have a couple of PHP functions that are supposed to pass information from one to another. The an AJAX call to the final function should produce the final result through that call. Its not happening. My count results in 0 and I am not understanding why. If I replace my query with a static array like [44,55,99,33] then it will pass the values ok. Likewise if I keep the query and simply replace the $id in the query and make it a static id such as '20' then it works. I would think that the id was the issue then and it wasnt pulling properly but it seems to work ok just not pass? function idx(){     global $wpdb, $id;       $getId = $_SERVER['REQUEST_URI'];     $ecp = explode('/', $getId);     if($ecp[3] != ''){         $id = $ecp[3];     }         return $id; } add_action( 'after_setup_theme', 'idx' );  =============================================== function Total_Result(){     global $wpdb;     $id = idx();        /*$resultx = [44,88,59,52];*/ ;      $resultx = $wpdb-&gt;get_results( $wpdb-&gt;prepare(&quot;SELECT * FROM {$wpdb-&gt;prefix}custom_form_submission_conversation as cc LEFT JOIN {$wpdb-&gt;prefix}custom_form_submission as cs on cc.fsid ='$id' WHERE fsid='$id'&quot; ));          $resultxs = array($resultx);          $total_result =  count($resultxs[0]);     echo  $total_result; }  when I var_dump $resultx I get: array(3) { [0]=&gt; object(stdClass)#1256 (36) { [“cid”]=&gt; string(3) “167” [“fsid”]=&gt; string(2) “20” [“specified_date”]=&gt; string(3) “yes” [“price_quote”]=&gt; string(4) “1140” [“posted_by”]=&gt; string(9) “dc_vendor” [“limit_count”]=&gt; string(1) “1” [“read_unread”]=&gt; string(1) “1” [“vendor_response”]=&gt; string(4) “zero” [“date_created”]=&gt; string(19) “2021-04-15 11:39:08” }   [1]=&gt; object(stdClass)#1257 (36) { [“cid”]=&gt; string(3) “186” [“fsid”]=&gt; string(2) “20” [“specified_date”]=&gt; string(3) “Yes” [“price_quote”]=&gt; string(3) “602” [“posted_by”]=&gt; string(8) “customer” [“limit_count”]=&gt; string(1) “1” [“read_unread”]=&gt; string(1) “1” [“vendor_response”]=&gt; string(4) “zero” [“date_created”]=&gt; string(19) “2021-04-15 11:39:08” }   [2]=&gt; object(stdClass)#1258 (36) { [“cid”]=&gt; string(3) “188” [“fsid”]=&gt; string(2) “20” [“specified_date”]=&gt; string(3) “yes” [“price_quote”]=&gt; string(4) “1017” [“posted_by”]=&gt; string(9) “dc_vendor” [“limit_count”]=&gt; string(1) “1” [“read_unread”]=&gt; string(1) “0” [“vendor_response”]=&gt; string(4) “zero” [“date_created”]=&gt; string(19) “2021-04-15 11:39:08” } }   so then I try and pass that data like so: function getvendorresponsestatus() {     global $wpdb; $id = idx();          $total_resultx = Total_Result($id);         die; }  Ive tried with and without the die();. The AJAX call is: setInterval(function(){     var ffiidd = jQuery('#ffiidd').val();     var ajaxurl = &quot;&lt;?php echo admin_url('admin-ajax.php'); ?&gt;&quot;;          jQuery.post(ajaxurl, {         action: &quot;vendorAjaxprog&quot;,         ffiidd: ffiidd     }, function(data) {         console.log(data);         jQuery.post(ajaxurl, {             action: &quot;getvendorresponsestatus&quot;, //&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; this is the action that calls the function             fsid : ffiidd         },         function(datas) {             console.log('+ datas);             jQuery('.cust_vendor_top').html(datas);         });         if(data == 0){}else{             jQuery('.ajaxreload').html(data);                                     }     });               }, 5000);/*==30 seconds==*/  So the question is why wont it pass along the query array information but it passes a basic array or it will pass if I change the id in the query to be static? I know the query is good because of when I var_dumped it, I have results before I get to the Total_Result() function.
64217
I have a custom post which has a meta tag that needs to be using a file upload (for video files). I am wondering, what is the correct way of adding an "Upload" button that points to the WordPress media uploader and sets the url of the selected uploaded file to the text field that caused that associates the upload button.  I am not looking for code on creating the actual meta tag option, but for a way to actually add a Wordpress media upload button properly.
39468
I have a form on my front-page. It sends the entered address to Google API and returns the JSON to wp_options. That works fine. I am using update_option to do that, so it rewrites the meta_key each time. How would I go about having each address entered remain?    $address = esc_html( $_POST['address'] );    //assign JSON object to var using  geocodeing function('address') input    $address_results = gmap_geocode($address);    // transfer data to options array   $options['py_name'] = $py_username;   $options['address'] = $address;   $options['coords']  = $address_results;    // create and name the option_name and set fields with $options array   update_option( 'row_name', $options );
16882
I am trying to write a WordPress plug-in. In my project I have three PHP files.   My plug-in file Widget for the plug-in Data installation   I need to install data table on plug-in activation. For that sake I wrote code to create table for my plug-in in data installation file. But I am not able to create data table using this file.  My code for include data installation file and widget file in my plug-in file is  define ( 'OTHER_FILES_PATH', plugin_dir_path( __FILE__ ) ); require_once( OTHER_FILES_PATH . '/data_installation.php' ); require_once( OTHER_FILES_PATH . '/plugin_widget.php' );   My problem is when I put my data installation code in my plug-in file it's working fine but when I put it in separate file (data installation) I am unable to create tables for my plug-in.  Please let me know if I am making any mistake.
111771
I have the following problem: When I want to edit the content of posts, Wordpress simply opens its standard page editor where I can add and arrange blocks, control the general page design and what not. That page editor comes with tons of controls and in my case also all kinds of additional parameter areas for various plugins (like Yoast SEO and Content Egg etc). Only among others can I edit the post content (text) there. What I would like Wordpress to do is to open a custom page where I can just edit the post content (text) and submit the changed text back to the underlying database. So how would I go about directing Wordpress to opening some custom page I have created where I can simply edit the post content, without all the editing facilities of Wordpress around? The custom post editing page would probably a form with a message field where I can edit and submit the post content. How exactly would I need to build that form so that the (edited) post content is stored back into the database when I click on the form's submit button?
2498
Does anyone know how o load wp_editor via AJAX in WordPress?  My markup and editor is getting loaded properly but editor controls are not getting loaded properly, it may be because Javascript is not running in AJAX call.  Any help would be appreciated.
78783
I have a Wordpress blog and am trying to implement the  image script. In short, I need to target all post images, swap out the src, width, &amp; height attributes with data-src, data-width, &amp; data-height attributes. I then need to duplicate the image line and wrap it in &lt;noscript&gt; tags. This is the structure I'm trying to have Wordpress generate/create:  &lt;img data-src="wordpress/image/url/pic.jpg" data-width="{get width of image with PHP &amp; pass-in that value here} data-height="{get height of image with PHP and pass-in that value here}" class="fs-img"&gt; &lt;noscript&gt;     &lt;img src="wordpress/image/url/pic.jpg"&gt; &lt;/noscript&gt;   I have searched the Wordpress codex and the best possible route I can find are to use filters (ie. 'get_image_tag' &amp; 'image_tag') for modifying the html that Wordpress outputs for each image. I'm thinking that one of these options should work, or that I can do some pattern matching with regex (I know, not ideal), throw in a preg_replace and then inject this back into the_content filter.  I have tried some of these options but cannot get any to work. Could someone please offer some help? Found one suggestion , but can't even get it to work!  'get_image_tag' attempt:  Found this particular one on the web, but it would need modified to fit my logic (see above structure)...can't make sense of what the preg_replace array is doing on my own.  &lt;?php function image_tag($html, $id, $alt, $title) {     return preg_replace(array(         '/'.str_replace('//','\/\/',get_bloginfo('url')).'/i',         '/\s+width="\d+"/i',         '/\s+height="\d+"/i',         '/alt=""/i'     ),     array(         '',         '',         '',         alt='"' . $title . '"'     ),     $html); } add_filter('get_image_tag', 'image_tag', 0, 4); ?&gt;   Another 'get_image_tag' attempt:  &lt;?php function get_image_tag($id, $alt, $title, $align, $size='full') {     list($width, $height, $type, $attr) = getimagesize($img_src);     $hwstring = image_hwstring($width, $height);      $class = 'align' . esc_attr($align) . ' size-' . esc_attr($size) . ' wp-image-' . $id;     $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);      $html = '&lt;img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" data-width="' . $width . '" data-height="' . $height . '" class="' . $class . ' fs-img" /&gt;';     $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size);      return $html; } ?&gt;   Pattern-matching attempt:  Tried creating my own regex on this one, but not sure if it's correct.  &lt;?php function restructure_imgs($content) {     global $post;     $pattern = "/&lt;img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)(|\")(.*?)&gt;/i";      list($width, $height, $type, $attr) = getimagesize($2$3.$4$5);     $hwstring = image_hwstring($width, $height);      $replacement = '&lt;img$1data-src=$2$3.$4$5 title="'.$post-&gt;post_title.'" data-width="'.$width.'" data-height="'.$height.'" class="fs-img"$6&gt;';     $content = preg_replace($pattern, $replacement, $content);     return $content; } add_filter('the_content', 'restructure_imgs'); ?&gt;   Unfortunately can't get any of these examples to work. Any help or sharing your pre-written scripts/functions would be much appreciated! Thanks for helping a student learn!!
70822
I am trying to add defualt comments to my custom post type.  I have added to my supports argument:  'supports' =&gt; array('title','editor','thumbnail','tags','post-formats','comments','revision'),   I've also tried a few functions like this:  function default_comments_on( $data ) { if( $data['post_type'] == 'your_custom_post_name' ) {     $data['comment_status'] = 1; }  return $data; } add_filter( 'wp_insert_post_data', 'default_comments_on' );   But nothing seems to work.Here is my custom post type:  function spark_register() { $labels = array(     'name' =&gt; _x('Spark Garden', 'post type general name'),     'singular_name' =&gt; _x('Spark', 'post type singular name'),     'add_new' =&gt; _x('Add New Spark', 'sparknz'),     'add_new_item' =&gt; __('Add New Spark'),     'edit_item' =&gt; __('Edit Spark'),     'new_item' =&gt; __('New Spark'),     'view_item' =&gt; __('View Spark'),     'search_items' =&gt; __('Search Sparks'),     'not_found' =&gt;  __('Nothing found'),     'not_found_in_trash' =&gt; __('Nothing found in Trash'),     'parent_item_colon' =&gt; ''   );  $args = array(     'labels' =&gt; $labels,     'public' =&gt; true,     'has_archive' =&gt; true,     'publicly_queryable' =&gt; true,     'show_ui' =&gt; true,     'query_var' =&gt; true,     'rewrite' =&gt; true,     'capability_type' =&gt; 'post',     'hierarchical' =&gt; false,     'menu_position' =&gt; 8,     'supports' =&gt; array('title','editor','thumbnail','tags','post-  formats','comments','revision'),           'taxonomies' =&gt; array('post_tag'), );  register_post_type( 'spark' , $args ); }   add_action('init', 'spark_register');   I've tried adding to 'twentysixteen' and activating but that still doesn't show the comments by default. I have it checked to allow comments in my settings, it does appear in my regular 'post' post type, but in my cutom post type the discussion checkbox is not checked and comments are not showing by default?  Thanks in advance..
27644
I might just be lazy, but do you ever find it tiresome to select all the source code from a large post on Stack&nbsp;Overflow to copy it?  I would like a feature, possibly a Firefox extension, but preferably a JavaScript script, for automatically copying source examples to the clipboard. It's no big deal for a small snippet, but a 1000+ line posting is cumbersome to copy.  Do you guys have a better way of copying large source posts? I'm using Firefox 3.5.5 on Windows&nbsp;7.
71974
I developed a plugin using the normal convention of naming the main plugin file after the plugin folder (i.e. prefix-someplugin for the folder and prefix-someplugin.php for the file). However, I saw some plugins that use the following structure:  prefix-someplugin  - plugin-loader.php // This file just loads src/prefix-someplugin.php  - /src     - prefix-someplugin.php   Obviously, if they used such structure, it means that it works. The issue is that I'm not sure how it works, hence my question. How does WP know which file to load as the main plugin file? I would have assumed that it looked for a file named after a folder, just adding .php as an extension, but it seems I was wrong.  Does WPjust scan wp-content/plugins for folders, and loads all the files it finds in them, or does it follow a different logic?  Thanks in advance for the answers.
23011
I have publications and I want to simply add year and month to the permalink. Currently the year and month of publication is added to the post as a meta value. I've set up publications as a custom post type and created taxonomy for the specific title of the publication. I would like the permalink to be:  publications/publication-title/year-published/month-published/name-of-post  I simply want to add the meta values for the publication year and month to the permalink.   So far, I've been able to pull the year and month published and add it to the url but unfortunately I keep getting a 404 when I try to access posts. I think I'm taking a wrong turn with one of my steps below ...  Any help would be appreciated.  These are the steps I did in the following order:   Since I was adding publication-year and publication-month to the url, I was reading that I had to make WordPress aware of these custom querystring variables. I added the following functions to register these custom variables:  function pubyear_register_rewrite_tag() {       add_rewrite_tag( '%pubyear%', '([0-9]{4})');   }    add_action( 'init', 'pubyear_register_rewrite_tag');  function pubmonth_register_rewrite_tag() {      add_rewrite_tag( '%pubmonth%', '([0-9]{2}');   }    add_action( 'init', 'pubmonth_register_rewrite_tag');    2) Added the year and month to the permalink structure:  add_filter('post_type_link', 'pub_term_permalink', 10, 4);  function pub_term_permalink($permalink, $post, $leavename, $sample) {     if ( false !== strpos( $permalink, '%publication-title%/%pubyear%/%pubmonth%' ) ) {       //get the publication-title      $publicationtype = get_the_terms( $post-&gt;ID, 'publication-type' );       //get the year of the publication      $pubyear = date('Y', get_post_meta($post-&gt;ID, 'pub_date', true));       //get the month of the publication      $pubmonth = date('m', get_post_meta($post-&gt;ID, 'pub_date', true));       $rewritecode = array(          '%publication-type%',          '%pubyear%',          '%pubmonth%',          $leavename? '' : '%postname%',      );       $rewritereplace = array(          array_pop($publicationtype)-&gt;slug,          $pubyear,          $pubmonth,          $post-&gt;post_name      );       $permalink = str_replace($rewritecode, $rewritereplace, $permalink);       }     return $permalink; }   3) Add rewrite rules:  function pub_add_rewrite_rules() {       add_rewrite_rule(  '^([^/]*)/([0-9]{4})/([0-9]{2})/([^/]+)?', 'publications/index.php?pagename=$matches[3]', 'top' );   }    add_action( 'init', 'pub_add_rewrite_rules' );   4) Made sure the custom post type is registered and includes this rewrite array:  'rewrite' =&gt; array     (             'slug' =&gt; 'publications/%publication-type%/%pubyear%/%pubmonth%',     'with_front' =&gt; false          ),
91212
I am trying to create a form that will allow users to update their information on the front end.  I am just starting to learn PHP (I know C# very well).  If I use this code to create a form on a WordPress page, how do I get the information from the user when they press the 'Update' button and then update the database?  function Insert_AccountTable_1() {     $current_user = wp_get_current_user();      if ($current_user-&gt;ID != 0)     {         return &lt;&lt;&lt;HTML             &lt;form class="AlumniForm" method="post"&gt;             &lt;h3&gt;Name&lt;/h3&gt;             &lt;br&gt;             &lt;table&gt;                 &lt;tbody&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="Username"&gt;Username&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;span&gt;$current_user-&gt;user_login&lt;/span&gt;&lt;/td&gt;                         &lt;td&gt;&lt;span class="description"&gt;Usernames cannot be changed.&lt;/span&gt;&lt;/td&gt;                     &lt;/tr&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="current_user_firstname"&gt;First Name&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;input id="current_user_firstname" type="text" name="current_user_firstname" value="$current_user-&gt;user_firstname"/&gt;&lt;/td&gt;                     &lt;/tr&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="current_user_lastname"&gt;Last Name&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;input id="current_user_lastname" name="current_user_lastname" type="text" value="$current_user-&gt;user_lastname"/&gt;&lt;/td&gt;                     &lt;/tr&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="current_user_displayname"&gt;Display Name&lt;span class="description"&gt;(required)&lt;/span&gt;&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;input id="current_user_displayname" type="text" value="$current_user-&gt;display_name"/&gt;&lt;/td&gt;                     &lt;/tr&gt;                 &lt;/tbody&gt;             &lt;/table&gt;              &lt;h3&gt;Contact Info&lt;/h3&gt;             &lt;br&gt;             &lt;table&gt;                 &lt;tbody&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="current_user_email"&gt;E-mail&lt;span class="description"&gt;(Required)&lt;/span&gt;&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;input id="current_user_email" type="text" value="$current_user-&gt;user_email"/&gt;&lt;/td&gt;                     &lt;/tr&gt;                     &lt;tr&gt;                         &lt;th&gt;&lt;label for="current_user_url"&gt;Website&lt;/label&gt;&lt;/th&gt;                         &lt;td&gt;&lt;input id="current_user_email" type="text" value="$current_user-&gt;user_url"/&gt;&lt;/td&gt;                     &lt;/tr&gt;                 &lt;/tbody&gt;             &lt;/table&gt;             &lt;input type="submit" name="current_user_submitupdates"&gt;Update&lt;/input&gt;             &lt;/form&gt; HTML;     }     else     {         return &lt;&lt;&lt;HTML         &lt;h3&gt;Error - User not logged in&lt;/h3&gt; HTML;     } }  add_shortcode('InsertAccountTableI', 'Insert_AccountTable_1');
1304
Is there an easy to understand scheme to decide what kind of code belongs to a plugin or the theme’s functions.php?  There    and many  about that topic, mostly because there are some misconceptions about the inner workings of WordPress. I am asking for an answer based on facts, not on opinions.  It should explain how to handle these points (and probably more):   custom post types and taxonomies contact forms shortcodes custom widgets  SEO functions like custom meta elements theme switch   There are often pros and cons for both sides. Our most popular question  got a lot of code snippets as answers that are at least debatable. We need criteria a beginner can understand, maybe a check list – with reasons.  See also the related question by Chip Bennett on our meta site:   Related:
78900
I recently updated my website after a while, and the custom fields meta box is not showing in the editor anymore. It isn't showing under "Screen Options" either. Any ideas why this could be, and how to get it back?