Forums

[resolved] Display (this) Based on Multiple Cat IDs from User Input (widget options) (4 posts)

  1. imscissors
    Member
    Posted 1 year ago #

    Hi,

    I've been searching high and low for how to do this for hours to no avail.

    I'm coding a plugin where my goal is to allow someone to place "whatever" on single posts based on category id's that they specify.

    Scenario
    ------------
    Place Google Adsense on post of these categories:

    In the widget options I provide a field to define category id's (comma separated):

    <!-- Multiple Category ID's -->
          <p>     
    
            <?php echo '<label for="' . $this->get_field_name('gaw_placebymcat') . '">' . __(' Multiple Categories by ID:') . '
            <input style="width:25%;" id="' . $this->get_field_id('gaw_placebymcat') . '" name="' . $this->get_field_name('gaw_placebymcat') . '"
            type="text" value="' . $gaw_placebymcat . '" /></label>'; ?>
    
          </p>

    So lets say their input is 6,11,17 - the person would want the widget (adsense ads) to display on all post in the categories 6,11, and 17.

    So I got the conditional tag in_category to correct return true or false based on the current posts categories by hard-coding the array as normal:

    if (in_category( array( '6', '11', '17' ) ) ) { echo 'display ads her because this is cat 6, 11 or 17';} else {echo 'we wont display because condition is false';}

    So I confirmed this as working. So it looks like I need an array like in the code above. So I looked how to create an array from a comma separated string and came up with this:

    $multicats = $gaw_placebymcat;
    //the variable is user defined cats sep'd by commas
    
    // split to array
    $multicatarray = explode(', ', $multicats);
    print_r($multicatarray);

    The result:

    Array ( [0] => 6,11,17 )

    But I don't think this what I want, and I have know idea how to incorporate that into the in_category condition. Again, I'm pretty new to PHP and a bit of a hack I guess that's how you learn :)

    Anyway, I'm so hard-headed I worked on this for 8 hours before asking here.

    Hope one of you can point in the right direction. Maybe I'm totally turned around here.

    So please, please help me!

    Thanks a bunch,
    Bryan

  2. Xephan
    Member
    Posted 1 year ago #

    Your explode() parameter is wrong, there's an extra space after the comma. So you end up with an array with only 1 value which is "6,11,17". The correct usage in your case should be
    $multicatarray=explode(',',$multicats);

    This will work regardless of whether your user inputs 6,11,17 or 6, 11, 17 or a mix of commas with or without spacing.

    After that, your in_category check should work.

    As a side note, I'll suggest you name your variables in full words and with camel capitalization or underscore separations. It will make it easier to figure out what you were doing a few months down the road. e.g.

    $multiCategoriesArray = explode(',', $multiCategories);

  3. imscissors
    Member
    Posted 1 year ago #

    Xephan,

    Nice! That did it... For anyone's future reference, here's the code:

    $multiCategories= $gaw_placebymcat;
    // move into usable array
    $multiCategoriesArray=explode(',',$multiCategories);

    Then the condition:

    //if this field has an input...
    if ($gaw_placebymcat != '') {
      //if the current cat matches one of the user input cat id's...
      if (in_category( $multiCategoriesArray) ) {
      // execute this...
      include ('the-widget-code.php'); }
    }

    Btw, thanks for the tips about for capitalization. Do you have a resource handy as sort of a guide to "clean coding"... Would love to have that.

    Thanks again bro,

    Bryan

  4. Xephan
    Member
    Posted 1 year ago #

    I don't have a single resource handy for clean coding, mine is a result of working with various languages over the past decades. The primary underlying principle is "Is my code easy for somebody to understand and debug a year down the road when even I might not remember why I did what I did the way I did"

    <Wall of Text Warning>
    In general,

    1. Leave comments explaining things that you had to spend time researching/testing to figure out.

    2. Name variables/functions in full so the situation won't arise when you meant "Cats" the animal (say while doing a plugin for a pet website) and get it confused with "Cats" for categories. Or wonder what is $gaw_placebymcat? ;)

    3. Proper indentations

    4. Do not mix code and html even though PHP allows it. It's messy, I know WordPress does it, but that's probably due to the origins of the project. Just see in the past week here how many people got confused about adding <?php ?> when they shouldn't. :)

    5. Be consistent, don't mix/switch styles midway, e.g. $gaw_placebymcat

    To get a more detailed grasp, you can refer to a coding conventions like these
    http://www.horde.org/horde/docs/?f=CODING_STANDARDS.html
    http://framework.zend.com/manual/en/coding-standard.overview.html

Topic Closed

This topic has been closed to new replies.

About this Topic