Support Forum

Able to add ID to sp_SectionStart()?

LH Lee H
Lee H
Member

So far I can only see how to add a class to sp_SectionStart(). Is there a way to add a ID as well?

I tried to figure out how to via the filters but can’t figure out how to without a preg_replace. Could we consider $rowId as a arg available if the switch goes to default? Or even a filter within default?

If not, theme/plugin devs will often have to resort to making divs outside of the api or is this not a concern?

15 Answers

New Answer

MP Mr Papa
Mr Papa
Member

We do add some IDs for specific page views… but we do not have a general argument for passing an ID…

out of curiosity, since section start is likely used many times on a page, what is the use case for an ID? sure one exists…

but yes, my expectation is that folks will quite often make divs outside of the api…  In fact, if I was writing a theme, unless I specifically need the benefit of a sectionStart, I would just add a standard html div tag…  I fully expect when folks start playing with themes, you will see a lot of non generic php populating the themes… the template files are indeed simply a php file so any php you want to add to control how the theme operates is fair game…  just look at a wp theme template file…  we have gone perhaps a bit overboard with the api to make it easier for some to read and follow… but as I said, I would expect the hardcore coders and themers to revert to php…

that said, I can chat with Andy about some IDs… the biggest issue will be that each ID will have to be unique in order to validate and we wont put that kind of checking into the api…

LH Lee H
Lee H
Member

I needed to use a ID to make css targeting more bulletproof in a widget div in the WP sidebar.

Going by the default template as an example, I thought I’d try to stick to the api as much as possible. I like how the section start has the hook with the dynamic name. Freaking clever!

do_action('sph_AfterSectionStart_'.$sectionName, $a);

While on the topic… how about adding one more filter to the function at the end?

    # output section starting div 
    if(!empty($rowId)) $rowId=" id='$rowId'"; 
    $out = "<div class='$tagClass$rowClass'$rowId>n"; 
 
    if ($echo) { 
        echo $out;

Becoming

    # output section starting div 
    if(!empty($rowId)) $rowId=" id='$rowId'"; 
    $out = "<div class='$tagClass$rowClass'$rowId>n";
    $out = apply_filters('sph_SectionStart', $out, $sectionName, $a); 
 
    if ($echo) { 
        echo $out;

Would be nice to be able to modify the output. Or eliminate empty divs left behind by rogue plugins that have removed items via hooks ‘n filters. (heh, heh)

Of course sp_SectionEnd and the column related ones would need similar filters laugh

YS Yellow Swordfish
Yellow Swordfish
Member

I think we are in agreement here that we would rather not give these sections id’s. If nothing else making each of them unique would probably undermine their worth. Having said that…

Steve: One thing we COULD do here is allow for a tagId to be passed as a parameter but define no default. Then we could include a section tagId only if it was explicitly passed. This would allow Lee to assign an id to a section and put the onus on the developer to ensure uniqueness. Any downside to that?

Lee: I don’t see any reason why your should not get your filter request.

Steve: If you agree with the filters request and se no problems I may have missed I can get those in.

LH Lee H
Lee H
Member

Thanks guys, it will really help me out. Hopefully someone else will benefit in the future.

YS Yellow Swordfish
Yellow Swordfish
Member

sp_SectionStart is the difficult one as we already place rowId on all of the pre-set views. So what are you expecting here? That a tagId passed would replace all of those? That would trip most people up as far as if uniqueness goes. Seemed like a good idea last night but I am starting to doubt it.

So – what the expectations?

LH Lee H
Lee H
Member

If a user doesn’t pass a $sectionName that doesn’t match any in the switch, it goes to default and no $rowID is created. So how about if there are no $sectionName matches, and the switch makes it to default, see if $tagID arg is set. If set make that the $rowID. But ONLY do that if $sectionName doesn’t match a preset.

Forgive me…  I’m not a explainer how about a example… Note tagID is added as a new arg.

function sp_SectionStart($args='', $sectionName='') { 
    $defs = array('tagClass'     => 'spPlainSection', 
            'echo'        => 1,
            'tagID'        => '', 
                        ); 
    $a = wp_parse_args($args, $defs); 
    $a = apply_filters('sph_SectionStart_args', $a); 
    extract($a, EXTR_SKIP); 
 
    # sanitize before use 
    $tagClass    = esc_attr($tagClass);
    $tagID        = esc_attr($tagID); 
    $echo        = (int) $echo; 
 
    # notifiy custom code before we start the section code 
    do_action('sph_BeforeSectionStart', $sectionName, $a); 
    do_action('sph_BeforeSectionStart_'.$sectionName, $a); 
 
    # specific formatting based on 'defined' names 
    $rowClass = ''; 
    $rowId = ''; 
    switch ($sectionName) { 
        case 'group': 
            global $spGroupView, $spThisGroup; 
 
            $rowClass = ($spGroupView->currentGroup % 2) ? ' spOdd' : ' spEven'; 
 
            $rowId = "group$spThisGroup->group_id"; 
            break; 
 
        case 'subforum': 
        case 'forum': 
            global $spGroupView, $spThisForum; 
 
            $rowClass = ($spGroupView->currentForum % 2) ? ' spOdd' : ' spEven'; 
            if ($spThisForum->forum_status) $rowClass.= ' spLockedForum'; 
 
            $rowId = "forum$spThisForum->forum_id"; 
            break; 
 
        case 'topic': 
            global $spForumView, $spThisTopic; 
            $rowClass = ($spForumView->currentTopic % 2) ? ' spOdd' : ' spEven'; 
            if ($spThisTopic->topic_status) $rowClass.= ' spLockedTopic'; 
            if ($spThisTopic->topic_pinned) $rowClass.= ' spPinnedTopic'; 
 
            $rowId = "topic$spThisTopic->topic_id"; 
            break; 
 
        case 'post': 
            global $spTopicView, $spThisPost; 
            $rowClass = ($spTopicView->currentPost % 2) ? ' spOdd' : ' spEven'; 
            if ($spThisPost->post_pinned) $rowClass.= ' spPinnedPost'; 
            $rowClass.= ' spType-'.$spThisPost->user->usertype; 
            if (!empty($spThisPost->user->rank)) $rowClass.= ' spRank-'.sp_create_slug($spThisPost->user->rank[0]['name'], 'rank', false); 
            if (!empty($spThisPost->user->special_rank)) { 
                foreach ($spThisPost->user->special_rank as $rank) { 
                    $rowClass.= ' spSpecialRank-'.sp_create_slug($rank['name'], 'rank', false); 
                } 
            } 
            if (!empty($spThisPost->user->memberships)) { 
                foreach ($spThisPost->user->memberships as $membership) { 
                    $rowClass.= ' spUsergroup-'.sp_create_slug($membership['usergroup_name'], 'usergroup', false); 
                } 
            } 
            $rowId = "post$spThisPost->post_id"; 
            break; 
 
        case 'list': 
            global $spListView, $spThisListTopic; 
            $rowClass = ($spListView->currentTopic % 2) ? ' spOdd' : ' spEven'; 
            $rowId = "topic$spThisListTopic->topic_id"; 
            break; 
 
        case 'usergroup': 
            global $spMembersList; 
 
            $rowClass = ($spMembersList->currentMemberGroup % 2) ? ' spOdd' : ' spEven'; 
            break; 
 
        case 'member': 
            global $spMembersList; 
 
            $rowClass = ($spMembersList->currentMember % 2) ? ' spOdd' : ' spEven'; 
            break; 
 
        case 'default':
            if(!empty($tagID)) $rowID = $tagID; 
            break; 
    } 
 
    # allow filtering of the row class 
    $rowClass = apply_filters('sph_SectionStartRowClass', $rowClass, $sectionName, $a); 
 
    # output section starting div 
    if(!empty($rowId)) $rowId=" id='$rowId'"; 
    $out = "<div class='$tagClass$rowClass'$rowId>n"; 
 
    if ($echo) { 
        echo $out; 
 
        # notifiy custom code that section has started 
        # only valid if content is echoed out ($display=1) 
        do_action('sph_AfterSectionStart', $sectionName, $a); 
        do_action('sph_AfterSectionStart_'.$sectionName, $a); 
    } else { 
        return $out; 
    } 
}
YS Yellow Swordfish
Yellow Swordfish
Member

And changes committed.

let me know if I missed anything. Or got it wrong of course!

LH Lee H
Lee H
Member

Holy Crap Andy! I checked the diff of your commit. What a pain! Thanks so much. Looks great and as I was hoping on the mentioned functions, and you took it all the way to the bottom of the file.

I’ll get a chance to play with the code later tonight. I’m sure it will do what I was hoping. Hopefully my bright idea didn’t break anything laugh

LH Lee H
Lee H
Member

I’ve a question about the function. I didn’t notice before. But in the switch…

Is the switch default line incorrectly written? Or is it intentional that $sectionName have the string “default” to work?

Currently it’s written as this:

        case 'default': 
            if(!empty($tagId)) $rowId=$tagId; 
            break; 
    }

or should it be this?

        default: 
            if(!empty($tagId)) $rowId=$tagId; 
            break; 
    }
YS Yellow Swordfish
Yellow Swordfish
Member

Ho ho but yes that looks wrong…

Or is it….. I will need to investigate as Steve did this but we might pass in the term ‘default’ which would make this right.

YS Yellow Swordfish
Yellow Swordfish
Member

Steve – you are going to have to chime in on this one…

As far as I can tell this is an error – unless you are passing the actual term ‘default’ from somewhere I have not found. You did most of the default handling so I need you to verify.

LH Lee H
Lee H
Member

Either way works for me. I’ll hold tight until Steve gives his two cents 🙂

Edit: Forgot to add it “may” be intentional as it appears in sp_ColumnStart as well. Though I am completely lost in it’s purpose?