Posts: 3,491
Threads: 106
Joined: Mar 2010
I want to read the parent of a slug in a plugin, if possible without reading the slug xml file.
- In the frontend I cannot use returnPageField($slug,'parent') before index-pretemplate (it's still not defined)
- In the backend (the plugin page) the function is available but doesn't work - I get these errors:
Code:
Notice: Undefined index: my-slug in [...]\admin\inc\caching_functions.php on line 114
Warning: array_key_exists() expects parameter 2 to be array, null given in [...]\admin\inc\caching_functions.php on line 114
(I suppose it's because $pagesArray has not been populated.)
Any other recommended way to do this?
Posts: 6,266
Threads: 181
Joined: Sep 2011
What version ?
Well its a plugin so I guess all versions preferably.
3.2 or 3.2.1+ ( dont recall ) load page cache on index-header now, and eventually we will have lazy loading.
for now you can probably just check $pagesArray and call
getPagesXmlValues(false) if not set first.
Posts: 3,491
Threads: 106
Joined: Mar 2010
Thank you, but as I only need to find out one parent, I prefer not calling getPagesXmlValues as it would be an extra file read (what I want to avoid).
I was testing with GS 3.2.1 but I've just tried with 3.2.0 and 3.1.2 too.
It seems that, when in index-pretemplate, returnPageField only works since 3.2.1 (so I assume the index-header cache loading was introduced in this version)
This is what I might do to get the parent slug:
Code:
// if version>3.2.0 and is frontend
if (function_exists('get_gs_version') && get_gs_version() >= '3.2.1' && (basename($_SERVER['PHP_SELF']) == 'index.php') ) {
... use returnPageField ...
} else {
.... read page xml ....
}
Posts: 6,266
Threads: 181
Joined: Sep 2011
why not just check if pagesarray is empty, and pull the the file manually if you need it. Instead of checking versions.
Posts: 3,491
Threads: 106
Joined: Mar 2010
I'm assuming that get_gs_version and returnPageField will work the same way in future GS versions.
I could check and directly get values from $pagesArray, but I think I cannot be so "sure" it will work the same (name, structure, contents) in the future (when GS uses lazy loading or whatever).
Posts: 6,266
Threads: 181
Joined: Sep 2011
It will always work the same. $pagesArray IS the global pagecache.
we might add wrappers to abstract it like getPageCache() and add lazy loading to it. ( in fact i already have most of this plumbed in for 3.3)
But you can use $pagesArray just fine.
Posts: 3,491
Threads: 106
Joined: Mar 2010
Is this like what you suggest?
Code:
global $pagesArray;
if ($pagesArray) {
... use returnPageField ....
} else {
... read xml file ...
}
Posts: 6,266
Threads: 181
Joined: Sep 2011
yup, you might have to use count() == 0
I am pretty sure caching_functions does a
$pagesArray = array();
So its an empty array when its initialized.
Posts: 3,491
Threads: 106
Joined: Mar 2010
2013-05-09, 00:37:10
(This post was last modified: 2013-05-09, 00:37:58 by Carlos.)
No need to use
count. An empty array evaluates to
false (I had tested and worked for me, but just in case I searched and found it's
how PHP works)
I'll use
if($pagesArray) then. Thank you for your comments!