WordPress.org

Ready to get started?Download WordPress

Forums

2 Quick Q's Regarding Post Manipulation (13 posts)

  1. jbh
    Member
    Posted 4 years ago #

    Hey,

    2 quick questions.

    1.) What is the best native method to retrieve a post ID
    outside of the loop? I have tried:
    global $post
    $post-ID;

    to no avail

    2.) If I wanted to identify if a token exists, such as:
    [level=5]

    do I just use native regex functions for PHPP or are built in ones available?

    My problem is some posts have 3 types of tokens and I am using some regex, some explode functions, and it's slowing down the site, esp. larger posts that need to load their blog data as well as the manipulation (I use the token values to work with a 3rd party calendar script and display that data afterward)

    Is there a list of native functions that can determine "yes/no" if a token exists (or any string) in a post that is wp native? I figure that might help me reduce load time dramatically.

    Thank you.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Outside the loop..

    get_the_ID();

    Though, this should also work..

    global $post;
    $post->ID;

    Notice the > and ; which was missing from your original code.

    And yet another alternative..

    global $posts;
    $posts[0]->ID; // [0] first key in the array of posts

    In regard to the tokens, can you provide an example(code form) of how and what you're checking?

  3. jbh
    Member
    Posted 4 years ago #

    Thanks, Mark. I'll try the ID trick in a moment. To answer your question, along with posting code, here is where I am...

    I have a page that has a postloop function. What it does is initially look for 3 page id's and, if it finds 1 of the 3 pages is loaded, it creates order forms. Outside of that loop, I include a page for calendar functionality (using a 3rd party calendar script)

    so I include that page such as

    include("calendar_page.php");

    I displayed it BEFORE the post_loop.

    In that page, I try to find 3 types of tokens:

    [listings_page]
    [total_events=5] // that # is hard coded when a user creates a page, it can be 5, 10 or anything they want.
    [categories_5|10|22]

    So calendar_page.php runs the functionality to find the tokens, remove them from visibility, and strip the values so it can display calendar data. It works, but it's slooow.

    I also include that code in another post_loop type function (different name) so I am using TWO filters, one for main.php and
    one for calendar pages. I wonder if that is causing issues?

    Anyway, here is some of the code:

    function calendar_replace($content)
    {
    global $user_ID;
    global $post;
    global $category;
    $thePostID = $post->ID;
    
    # Some code to determine variables for the calendar script
    
    # Following code is to determine values in the TOKENS
    $list_category=explode("[category=",$content);
    $category_piece=$list_category[1];
    $list_category_total=explode("]",$category_piece);
    $category=$list_category_total[0];
    
    # Let's do it now for Events
    
    # SAME CONCEPT - Explode/etc to find values in tokens
    # Will save you from reading all of the code..
    # I then use the rest of the code to find other vals I need
    # that are not coming from tokens.
    
    # Now, we create url's that we can include which involves
    # finding a url with the 3rd party calendar script
    # so we can display events based on the tokenized data
    # in this page's post
    
            $url1="http://domain.com/calendar-2/show_listings.php?mo=$mo&yr=$yr&cat=$cat=$category&total_listings=$total_events";
    
            $url2="http://domain.com/calendar-2/demo.php?mo=$mo&yr=$yr&total_listings=$total_events&showCat=$sc&view_method=$ext";
    
    $url3="http://domain.com/calendar-2/demo_new.php?mo=$mo&yr=$yr&total_listings=$total_events&showCat=$category&view_method=list";
    
      $request = new WP_Http;
      $result = $request->request( $url1 );
      $result2 = $request->request( $url2 );
      $result3 = $request->request( $url3 );
    
    # At this point, we just have to tell the script to
    # replace the post's content with the added 3rd party
    # calendar data, which was pulled from the 3 url's above
    
    $content=str_replace("[calendar_events]",$r1,$content);
    $content=str_replace("[calendar]",$r2,$content);
    $content=str_replace("[listings_page]",$r3,$content);
    
    # Then we return $content and add the filter.

    So it's main.php loading the normal pages, having it's own post loop and filter for 'the_content'. Inside this page, outside of the loop (at the bottom, after the filter) I include the calendar page

    The calendar page uses explode/php functions to strip out the values in tokens, I create the urls (dynamic urls) to display 'event data' for each page and then I add that to another filter for 'the_content'

    TY for your time!

  4. jbh
    Member
    Posted 4 years ago #

    If I need to create 2 txt files with full code I can
    but I figured this helps explain why it might be delayed.

    My theories are:

    A.) Too poor of a design to strip the token data (explode)
    B.) Two filters for these pages. The pages that load slowly are the ones with the TOKENS. Do the two filters for 'the_content' add to the load time, maybe?

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    So your tokens they are just shortcodes inside the given post or page, and what you're doing is simply checking for the presenece of the given code and running some function when one or several are detected.

    There's an API in place for adding shortcodes, so my first question is, are your custom codes registered as shortcodes?
    http://codex.wordpress.org/Shortcode_API

    At least that way you leverage the existing api in filtering through content and acting on a given code, ie. [mycode].

  6. jbh
    Member
    Posted 4 years ago #

    My tokens are not registered as shortcodes. It's this part of the api i am not familiar with.

    Yeah, each page that has a token is given one so I can use that data to interact with the 3rd party calendar script. I'll research that link.

    One question. Does having a potential 'double the_content' filter, as well as explode statements, for posts with a fair amount of text to sift through, cause these delays?

    Pages without the tokens load quickly, btw

    Thanks. I've lost a full weekend dealing with this. I appreciate your time.

  7. jbh
    Member
    Posted 4 years ago #

    The dream is to have main.php run most of the pages/posts and do nothing other than filter through the_content with the post_loop function.

    Then, at the same time, detect IF a token exists in a page and if not, I include a separate page with their own the_content filter.

    So it's not possibly loading 2 at once/etc.

    It's the inability to quickly, without a million explode statements/etc, determine IF a page that the user visits has a token that is causing me headaches.

    Ty , again. I hope that I am clear.

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    It could possibly be the HTTP requests your function runs that causes the delay, ie. $result2 = $request->request( SOME_URL ), but beyond that i'd be making a wild guess as to the precise cause of slow load times.

    Only real way to know what causes the biggest slow down would be to comment out pieces of code and see how performance differs when they don't run, though i appreciate that's not a realistic test practice on a live website.

    With any kind of shortcode, i'd suggest the first thing your code should do is check for the presense of the shortcode(tag/attribute) before doing anything else (such as explode), ie..

    if( false !== strpos( $content_to_check, '[mycode' ) ) {
       // Do processing
    }

    I'm about to head off to bed shortly, but i'll check back tomorrow when my head is clearer (a little inebriated right now)... :)

  9. jbh
    Member
    Posted 4 years ago #

    lol, thanks. I appreciate it. I will use a 'sniffer' like that as well as continue to learn more about the shortcode api

    The reason I use the http method is because I need to turn the token
    into the url and it doesn't allow me to include dynamic full url's on the server. I tried curl earlier and the load time was horrendous.

    Ty, for now.

  10. jbh
    Member
    Posted 4 years ago #

    Yeah, I am not using conditionals with those connectors, such as "If this page has this token, connect with this $result.

    So it's probably connecting three times (3 connections for 3 unique types of tokens used for 3 types of pages) and causing that delay. If I sniff out the token and use a conditional to assign one connection per page/token, I might see a difference...

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    Perhaps a nice way to check areas of concern would be to use a simple time script to measure how long particular areas of code take to execute, something simple like the examples here..

    http://php.net/manual/en/function.microtime.php

    Where the code currently has usleep(100) would be where your code sits..

    You could simply encase differing pieces of code and see when the time comes back bigger, and obviously if you're testing this on a live site then instead of..

    echo "Did nothing in $time seconds\n";

    You could do something like.. echo "<!-- Did nothing in $time seconds -->"; so the result is output into the source of the page (out of visitor view).

    Should be helpful for at least narrowing down which pieces of code cause the longest delays.

  12. jbh
    Member
    Posted 4 years ago #

    The delay is the connectivity issue and then the way I manipulate the entire post of these long pages to look for tokens. I am building functions to use shortcode method that you used so I can pull and remove data more efficiently.

    One method I am experimenting with is

    $response = wp_remote_get($url3);

    Problem is when I try to replace a token through $content and
    output it (on a test site) it's now not showing it:

    function shortcode_plain() {
    
    return 'plain token';
    
    }
    
    # add_shortcode('listings_page','shortcode_plain');
    
    # I'd like to use shortcode to strip out a value somehow
    # turn [total_listings=5] into 5
    
    # Of course, since this api exists, maybe it's best to just change the tokens to apply to
    # what it expects. That is the perfect last resort, I'd wager.
    # It looks as if shortcode_example2 can do it
    
    function shortcode_stripvalue($attr) {
    
    echo "Yes, We Were Able To Strip From [total events]!<br>";
    
    $url3="http://www.site.com/calendar-2/demo_new.php?mo=$mo&yr=$yr&total_listings=$total_events&showCat=$category&view_method=list"
    
    $response = wp_remote_get($url3);
     $content=str_replace("[listings_page]",$r3,$content);
    
    return $content;
    
    }
    
    add_shortcode('total','shortcode_stripvalue');
    
    add_filter('the_content',shortcode_stripvlaue);

    Now it shows nothing. Strange...

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    What is the value of the shortcode when you test, based on the above, [listings_page] should produce plain token (if you want to feed it parameters, they need to write those into your shortcode function, as per the examples on the shortcode page).

    the_content i think (not sure without looking) is the content - post filters (once shortcodes have been replaced, etc), so your filter won't find a match for [listings_page], it should find a match for plain token though.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags