Forums

[resolved] Entry in a Custom Field becomes a Tag or Category (14 posts)

  1. doppledoer
    Member
    Posted 1 year ago #

    I am using the More Fields plugin and it works great. However, I would like to make it so that whatever is entered in a certain custom field becomes a tag or category.

    This would make submitting a post much easier for other inexperienced authors because I could set up nice custom fields for everything they need to enter and still keep everything organized by tags and categories.

    Is it possible to insert something relatively simple in one of the php pages so that these custom fields became tags or categories?

    Any ideas how?

    Thanks in advance,

    Peter

  2. MichaelH
    moderator
    Posted 1 year ago #

    Could use a plugin such as Get Custom Fields and then use the c2c_get_custom function to form a url that links to your category or tag archives.

  3. doppledoer
    Member
    Posted 1 year ago #

    I'm not really sure what you mean by 'links to your category or tag archives'.

    Basically, I would like to display posts, depending on a custom field value. I could do this using this method, however it would be rather complex, especially if doing it for multiple instances.

    What I would like is for the custom field value to become a tag or category, so i could then use the query_posts function to quickly manipulate the output of posts.

    You may say 'just enter the values in the tag box like everyone else' but I have good reason for wanting to do this. It would make submitting posts to a strict standard much easier for inexperienced authors.

    So, anyone have ideas?

    Thanks in advance.

  4. MichaelH
    moderator
    Posted 1 year ago #

    Actually, my suggestion is totally useless cause even though you might come up with a URL derived from the custom field, the post won't appear in a category or tag archive so forget what I suggested...

    If you expect users to have a problem with tags, then entering custom fields will not be too much for them either.

    Maybe a tag plugin that makes tagging easier is the way to go. Like Simple Tags.

  5. henrikmelin
    Member
    Posted 1 year ago #

    You can do this by inserting javascript in the admin footer, i.e. with a plugin looking something like this would do it:

    <?php
    /*
    Plugin Name: Meta to Tag
    Version: 0.1
    Plugin URI: http://labs.dagensskiva.com/
    Description: Creates a tag for a specific key created in More Fields.
    Author: Henrik
    */
    function set_tag_by_meta () {
      // This is the key created in More Fields that will also become a tag.
      $the_key = 'dsc_artist';
    
      if (preg_match("/\/post/", $_SERVER['PHP_SELF'])) {
        ?>
        <script language="JavaScript" type="text/javascript">
        document.onload = p2m_init();
    	// Hook functions to the key
        function p2m_init () {
        	$('<?php echo $the_key; ?>').onchange = function () {
              var tags = $("tags-input").value;
              $('tags-input').value = $('<?php echo $the_key; ?>').value + ', ' + tags;
            };
          }
          </script>
        <?php
      }
    }
    add_action('admin_footer', 'set_tag_by_meta', 11);
    
    ?>

    Which would take the value of a key created in More Fields and add it to the tag list.

    Hope that helps.

    Henrik.

  6. doppledoer
    Member
    Posted 1 year ago #

    Thanks Henrik, it does the job nicely. Excellent work.

    The only problem is that I can't work out how to adapt it so that it works for multiple custom fields (with the least amount of doubling up). I tried just copying various aspects of it, e.g. creating $the_key2 = 'dsc_artist2'; etc. but it didn't work.

    I'm also wondering if it is possible to do the same with categories? For example, I might create a category called 'album' and a custom field called 'cd type' which would be a drop-down list. If 'album' was selected from the list, that post would also be entered into the 'album' category. I imagine it would be similar to above.

    Of course, this could probably be avoided if there was a plugin out there that allowed you to choose categories on the 'write post' page using drop-down menus, or at least gave you more flexibility. Unfortunately, I haven't been able to find one.

    Any help with this would be greatly appreciated,

    Cheers,

    Peter

  7. henrikmelin
    Member
    Posted 1 year ago #

    To make it do the same for more than one key, just copy and paste the three lines after 'function p2m_innit', then replacing $the_key with $the_key_2, making sure you set the value of $the_key_2.

    Yeh, you're right, it is possible to do similar things for categories, creating a box in More Fields containing a select list with the values of the categories. You can then, attach JavaScript functions to the select field that tick, or untick the WP category boxes.

    The JavaScript might look something like this (untested):

    Let the key of the select list be 'dsc_format'.

    $('dsc_format').onchange = function () {
    
      // Get the value in the select list
      var cat_value = $('dsc_format').value; 
    
      // The 'album' category...
      var album_cat_id = 'in-category-<?php echo get_cat_id('album'); ?>';
      if (cat_value == 'album') $(album_cat_id).checked = true;
      else $(album_cat_id).checked = false;
    
      // The 'single' category...
      var single_cat_id = 'in-category-<?php echo get_cat_id('single'); ?>';
      if (cat_value == 'single') $(single_cat_id).checked = true;
      else $(single_cat_id).checked = false;
    
      // and so on ...
    
    }
  8. doppledoer
    Member
    Posted 1 year ago #

    Again, thanks for your help Henrik. However, I can't get it to work for categories. My knowledge of Javascript and the backend of Wordpress plugins is pretty thin, so I tried adapting the original Meta to Tag plugin you put forward, but without luck. Perhaps I did something obviously wrong? This is what it looked like:

    <?php
    /*
    Plugin Name: Meta to Cat
    Version: 0.1
    Plugin URI: http://labs.dagensskiva.com/
    Description: Creates a cat for a specific key created in More Fields.
    Author: Henrik
    */
    function set_cat_by_meta () {
    
      if (preg_match("/\/post/", $_SERVER['PHP_SELF'])) {
        ?>
        <script language="JavaScript" type="text/javascript">
        document.onload = p2m_init();
    	// Hook functions to the key
        function p2m_init () {
    
    $('rate_pop').onchange = function () {
    
      // Get the value in the select list
      var cat_value = $('rate_pop').value; 
    
      // The 'Nobody Cares' category...
      var nobody_cat_id = 'in-category-19';
      if (cat_value == 'nobody') $(nobody_cat_id).checked = true;
      else $(nobody_cat_id).checked = false;
    
      // The 'Mostly Friends' category...
      var friends_cat_id = 'in-category-18';
      if (cat_value == 'friends') $(friends_cat_id).checked = true;
      else $(friends_cat_id).checked = false;
    
      // The 'Decent Following' category...
      var decent_cat_id = 'in-category-16';
      if (cat_value == 'decent') $(decent_cat_id).checked = true;
      else $(decent_cat_id).checked = false;
    
      // The 'Big Puller' category...
      var puller_cat_id = 'in-category-20';
      if (cat_value == 'puller') $(puller_cat_id).checked = true;
      else $(puller_cat_id).checked = false;
    
      // The 'Stadium Filler' category...
      var filler_cat_id = 'in-category-17';
      if (cat_value == 'filler') $(filler_cat_id).checked = true;
      else $(filler_cat_id).checked = false;
    
      // and so on ...
    
    };
    
    }
    
          </script>
        <?php
      }
    }
    add_action('admin_footer', 'set_cat_by_meta', 11);
    
    ?>

    Know what might be wrong?

    Thanks

  9. henrikmelin
    Member
    Posted 1 year ago #

    Well, that works for me... What is your Error Console telling you (in Firefox)? Make sure 'rate_pop' is the key in More Fields for the select list.

    Henrik.

  10. doppledoer
    Member
    Posted 1 year ago #

    I'm not getting any errors, it just isn't doing anything. I'll take another look,

  11. doppledoer
    Member
    Posted 1 year ago #

    Ok so I tried it and it worked, but only for the first choice in list ie the 'nobody' category. Perhaps it is interfering with the subsequent javascript. Is that what happens to you?

    Cheers (really sorry for all this trouble!)

  12. doppledoer
    Member
    Posted 1 year ago #

    I discovered the problem. I didn't realise dropdown lists had to be written as optionA,optionB,optionC rather than optionA, optionB, optionC. All good!

    Thanks Henrik. I really appreciate it. Although I must admit, I've already got some more issues that I'll probably be bothering you with in the very near future. Sorry!

  13. henrikmelin
    Member
    Posted 1 year ago #

    Ok, so I should probably trim any whitespaces off the left and right of the select values, then - tha'd make a lot more sense.

  14. upekshapriya
    Member
    Posted 1 year ago #

    This plugin code above (Meta to Cat) is exactly what I need but I get a console error showing in Firebug and no action!

    missing ; before statement
    dispatchCallback(null, "oninit", "onInit")tiny_mce_gzip.php (line 97)
    onLoad()tiny_mce_gzip.php (line 63)
    onLoad()tiny_mce_gzip.php (line 44)
    [Break on this error] var meditation-and-buddhism_cat_id = 'in-category-6';\n

    The code as I 'translated' the plugin for my categories is:

    <?php
    /*
    Plugin Name: Meta to Cat
    Version: 0.1
    Plugin URI: http://labs.dagensskiva.com/
    Description: Creates a cat for a specific key.
    Author: Henrik
    */
    function set_cat_by_meta () {
    
      if (preg_match("/\/post/", $_SERVER['PHP_SELF'])) {
        ?>
        <script language="JavaScript" type="text/javascript">
        document.onload = p2m_init();
    	// Hook functions to the key
        function p2m_init () {
    
    $('Event Type').onchange = function () {
    
      // Get the value in the select list
      var cat_value = $('Event Type').value; 
    
      // The 'Meditation and Buddhism' category...
      var meditation-and-buddhism_cat_id = 'in-category-<?php echo get_cat_id('meditation and buddhism'); ?>';
      if (cat_value == 'Meditation and Buddhism') $(meditation-and-buddhism_cat_id).checked = true;
      else $(meditation-and-buddhism_cat_id).checked = false;
    
      // The 'Buddhism' category...
      var buddhism_cat_id = 'in-category-<?php echo get_cat_id('buddhism'); ?>';
      if (cat_value == 'Buddhism') $(buddhism_cat_id).checked = true;
      else $(buddhism_cat_id).checked = false;
    
      // The 'Meditation' category...
      var meditation_cat_id = 'in-category-<?php echo get_cat_id('meditation'); ?>';
      if (cat_value == 'Meditation') $(meditation_cat_id).checked = true;
      else $(meditation_cat_id).checked = false;
    
      // The 'Retreats' category...
      var retreats_cat_id = 'in-category-<?php echo get_cat_id('retreats'); ?>';
      if (cat_value == 'Retreats') $(retreatsr_cat_id).checked = true;
      else $(retreats_cat_id).checked = false;
    
    };
    
    $('Level').onchange = function () {
    
      // Get the value in the select list
      var cat_value = $('Level').value; 
    
      // The 'Newcomer' category...
      var newcomer_cat_id = 'in-category-<?php echo get_cat_id('newcomer'); ?>';
      if (cat_value == 'Newcomer') $(newcomer_cat_id).checked = true;
      else $(newcomer_cat_id).checked = false;
    
      // The 'Friend' category...
      var friend_cat_id = 'in-category-<?php echo get_cat_id('friend'); ?>';
      if (cat_value == 'Friend') $(friend_cat_id).checked = true;
      else $(friend_cat_id).checked = false;
    
      // The 'Sangha' category...
      var sangha_cat_id = 'in-category-<?php echo get_cat_id('sangha'); ?>';
      if (cat_value == 'Sangha') $(sangha_cat_id).checked = true;
      else $(sangha_cat_id).checked = false;
    
    };
    
    }
    
          </script>
        <?php
      }
    }
    add_action('admin_footer', 'set_cat_by_meta', 11);
    
    ?>

    Any ideas what I've done wrong? (there are no extra whitespaces in my custom tags) I can't see where there could be another ; put! I've tried changing single quotes to double quotes and adding extra double quotes but it looks like the php is being processed ok.

    I'm not at all familiar with javascript so I may have made a silly mistake - I thought it might be to do with repeating the code for the second custom tag but that does not appear to be the case through testing.

    Maybe there is a conflict with TinyMCE? I am using Wordpress 2.3.3 on xampp.

Topic Closed

This topic has been closed to new replies.

About this Topic