1"use strict";(globalThis.webpackChunkbuilderius=globalThis.webpackChunkbuilderius||[]).push([[675],{3770:(n,e,t)=>{t.r(e),t.d(e,{assets:()=>c,contentTitle:()=>l,default:()=>h,frontMatter:()=>i,metadata:()=>s,toc:()=>p});const s=JSON.parse('{"id":"dynamic-data/query-examples/php","title":"PhP Function Output","description":"Call any PHP function directly from GraphQL queries using phpfunctionoutput() Pro Feature. This extends your queries with core PHP functions, WordPress functions, plugin functions, and custom PHP functions.","source":"@site/docs/dynamic-data/query-examples/php.md","sourceDirName":"dynamic-data/query-examples","slug":"/dynamic-data/query-examples/php","permalink":"/docs/dynamic-data/query-examples/php","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"id":"php","title":"PhP Function Output"},"sidebar":"docs","previous":{"title":"Meta Box in Custom Queries","permalink":"/docs/dynamic-data/query-examples/metabox"},"next":{"title":"Expression Result","permalink":"/docs/dynamic-data/query-examples/expressions"}}');var r=t(4848),o=t(8453),a=t(9489),u=t(7227);const i={id:"php",title:"PhP Function Output"},l=void 0,c={},p=[{value:"PHP Function Output Syntax",id:"php-function-output-syntax",level:2},{value:"WordPress Functions",id:"wordpress-functions",level:2},{value:"Plugin Integration Examples",id:"plugin-integration-examples",level:2},{value:"Complex Argument Structure",id:"complex-argument-structure",level:2},{value:"GraphQL Variables in PHP Function Output",id:"graphql-variables-in-php-function-output",level:2}];function d(n){const e={admonition:"admonition",code:"code",h2:"h2",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,o.R)(),...n.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(e.p,{children:["Call any PHP function directly from GraphQL queries using ",(0,r.jsx)(e.code,{children:"php_function_output()"})," ",(0,r.jsx)("span",{class:"badge pro",children:"Pro Feature"}),". This extends your queries with ",(0,r.jsx)(e.strong,{children:"core PHP functions"}),", ",(0,r.jsx)(e.strong,{children:"WordPress functions"}),", ",(0,r.jsx)(e.strong,{children:"plugin functions"}),", and ",(0,r.jsx)(e.strong,{children:"custom PHP functions"}),"."]}),"\n",(0,r.jsx)(e.h2,{id:"php-function-output-syntax",children:"PHP Function Output Syntax"}),"\n",(0,r.jsxs)(a.A,{groupId:"php-function-syntax",defaultValue:"syntax",values:[{label:"Syntax",value:"syntax"},{label:"Example",value:"example"},{label:"JSON Output",value:"json-output"}],children:[(0,r.jsxs)(u.A,{value:"syntax",children:[(0,r.jsxs)(e.p,{children:["Call any PHP function directly from GraphQL queries using ",(0,r.jsx)(e.code,{children:"php_function_output()"}),":"]}),(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"General syntax:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'php_function_output(\n function: "function_name"\n arguments: [arg1, arg2, arg3]\n fallback: default_value\n)\n'})}),(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Parameters:"})}),(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"function"})," ",(0,r.jsx)(e.strong,{children:"(required)"})," - Name of the PHP function to call"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"arguments"})," (optional) - Array of arguments to pass to the function"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"fallback"})," (optional) - Default value if function fails or doesn't exist"]}),"\n"]})]}),(0,r.jsxs)(u.A,{value:"example",children:[(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Basic usage examples:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Function with no arguments\n current_time: php_function_output(function: "time")\n \n # Function with single argument\n uppercase: php_function_output(\n function: "strtoupper",\n arguments: ["hello world"]\n )\n \n # Function with multiple arguments\n formatted_date: php_function_output(\n function: "date",\n arguments: ["Y-m-d H:i:s", 1234567890]\n )\n \n # Function with fallback value\
1n safe_option: php_function_output(\n function: "get_option",\n arguments: ["my_custom_option"],\n fallback: "default_value"\n )\n}\n'})})]}),(0,r.jsxs)(u.A,{value:"json-output",children:[(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"JSON response from the example:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-json",children:'{\n "current_time": 1701234567,\n "uppercase": "HELLO WORLD",\n "formatted_date": "2009-02-13 23:31:30",\n "safe_option": "default_value"\n}\n'})}),(0,r.jsx)(e.p,{children:"The function returns whatever the PHP function returns - could be strings, numbers, arrays, objects, or boolean values depending on the function called."})]})]}),"\n",(0,r.jsx)(e.h2,{id:"wordpress-functions",children:"WordPress Functions"}),"\n",(0,r.jsxs)(a.A,{groupId:"wordpress-functions",defaultValue:"simple",values:[{label:"Simple",value:"simple"},{label:"With Arguments",value:"with-arguments"},{label:"Post Context",value:"post-context"}],children:[(0,r.jsxs)(u.A,{value:"simple",children:[(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Functions that don't require arguments:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Check if user is logged in\n is_logged_in: php_function_output(function: "is_user_logged_in")\n \n # Get current user ID\n current_user_id: php_function_output(function: "get_current_user_id")\n \n # Get site information\n site_logo: php_function_output(function: "get_site_icon_url")\n site_url: php_function_output(function: "home_url")\n}\n'})})]}),(0,r.jsxs)(u.A,{value:"with-arguments",children:[(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Functions that need parameters:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Get specific option values\n contact_email: php_function_output(\n function: "get_option",\n arguments: ["admin_email"]\n )\n \n # Get theme customizer values\n header_color: php_function_output(\n function: "get_theme_mod",\n arguments: ["header_textcolor"],\n fallback: "#000000"\n )\n \n # Check if plugin is active\n woo_active: php_function_output(\n function: "is_plugin_active",\n arguments: ["woocommerce/woocommerce.php"]\n )\n \n # Get permalink for specific post\n about_page_url: php_function_output(\n function: "get_permalink",\n arguments: [21]\n )\n}\n'})})]}),(0,r.jsxs)(u.A,{value:"post-context",children:[(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Using current post data in function calls:"})}),(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n post {\n ID\n post_title\n \n # Get post meta values\n view_count: php_function_output(\n function: "get_post_meta",\n arguments: ["{{wp.post.ID}}", "post_views", true]\n )\n \n # Get categories for current post\n category_names: php_function_output(\n function: "wp_get_post_categories",\n arguments: ["{{wp.post.ID}}", {"fields": "names"}]\n )\n \n # Check if post has specific tag\n has_featured_tag: php_function_output(\n function: "has_tag",\n arguments: ["featured", "{{wp.post.ID}}"]\n )\n }\n}\n'})})]})]}),"\n",(0,r.jsx)(e.h2,{id:"plugin-integration-examples",children:"Plugin Integration Examples"}),"\n",(0,r.jsx)(e.p,{children:"Access plugin-specific functions for extended functionality:"}),"\n",(0,r.jsxs)(a.A,{groupId:"plugin-examples",defaultValue:"acf",values:[{label:"ACF Functions",value:"acf"},{label:"WooCommerce",value:"woocommerce"},{label:"MetaBox",value:"metabox"}],children:[(0,r.jsx)(u.A,{value:"acf",children:(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Get ACF options page values\n company_logo: php_function_output(\n function: "get_field",\n arguments: ["company_logo", "option"]\n )\n \n # Get all field groups\n field_groups: php_function_output(\n function: "acf_get_field_groups"\n )\n \n # Get field configuration\n hero_field_config: php_function_output(\n function: "acf_get_field",\n arguments: ["field_hero_image"]\n )\n \n post {\n # Get formatted ACF value\n formatted_date: php_function_output(\n function: "get_field",\n arguments: ["event_date", "{{wp.post.ID}}", false]\n )\n }\n}\n'})})}),(0,r.jsx)(u.A,{value:"woocommerce",children:(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n post {\n # Get product data (if current post is a product)\n product_price: php_function_output(\n function: "get_post_meta",\n arguments: ["{{wp.post.ID}}", "_price", true]\n )\n \n sale_price: php_function_output(\n function: "get_post_meta",\n arguments: ["{{wp.post.ID}}", "_sale_price", true]\n )\n \n is_on_sale: php_function_output(\n function: "get_post_meta",\n arguments: ["{{wp.post.ID}}", "_sale_price", true]\n )\n }\n \n # Get WooCommerce settings\n currency: php_function_output(\n function: "get_woocommerce_currency"\n )\n \n # Check if WooCommerce page\n is_shop: php_function_output(function: "is_shop")\n is_cart: php_function_output(function: "is_cart")\n}\n'})})}),(0,r.jsx)(u.A,{value:"metabox",children:(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Get MetaBox field settings\n contact_field_settings: php_function_output(\n function: "rwmb_get_field_settings",\n arguments: ["contact_email"]\n )\n \n # Get all fields from a meta box\n contact_fields: php_function_output(\n function: "rwmb_get_object_fields",\n arguments: ["contact_info", "post"]\n )\n \n post {\n # Get formatted MetaBox value\n formatted_content: php_function_output(\n function: "rwmb_the_value",\n arguments: ["custom_content", "", "{{wp.post.ID}}"]\n )\n }\n}\n'})})})]}),"\n",(0,r.jsx)(e.h2,{id:"complex-argument-structure",children:"Complex Argument Structure"}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n # Get related posts & exclude current post\n post {\n ID\n related: php_function_output(\n function: "get_posts",\n arguments: [{\n post_type: "post",\n posts_per_page: 3,\n post_not_in: ["{{ID}}"]\n }]\n )\n }\n}\n'})}),"\n",(0,r.jsx)(e.h2,{id:"graphql-variables-in-php-function-output",children:"GraphQL Variables in PHP Function Output"}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Variable syntax"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"{{field_name}}"})," - Data from current object (post, user, term)"]}),"\n",(0,r.jsxs)(e.li,{children:[(0,r.jsx)(e.code,{children:"[[global_variable]]"})," - Site-wide data or custom query results"]}),"\n"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-graphql",children:'{\n post {\n ID @private\n post_title @private\n \n # Use current post title and site title from built-in Data Variable in function\n custom_title: php_function_output(\n function: "sprintf",\n arguments: ["%s - %s", "{{post_title}}", "[[wp.site_title]]"]\n )\n }\n}\n'})}),"\n",(0,r.jsxs)(e.admonition,{title:"Security Features",type:"info",children:[(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.code,{children:"php_function_output"})," ",(0,r.jsx)(e.strong,{children:"applies security restrictions to prevent database modifications:"}),"\nThis is an important Builderius feature, and prevents a whole class of security breaches that plague other visual builders."]}),(0,r.jsxs)(e.p,{children:["\u274c You cannot use ",(0,r.jsx)(e.code,{children:"wp_insert_post()"}),", ",(0,r.jsx)(e.code,{children:"wp_update_post()"})]}),(0,r.jsxs)(e.p,{children:["\u274c You cannot use ",(0,r.jsx)(e.code,{children:"wp_insert_user()"}),", ",(0,r.jsx)(e.code,{children:"update_user_meta()"})]}),(0,r.jsx)(e.p,{children:"\u274c You cannot execute INSERT, UPDATE, DELETE queries"}),(0,r.jsxs)(e.p,{children:["\u2705 You can use any read-only function (",(0,r.jsx)(e.code,{children:"get_*"}),", ",(0,r.jsx)(e.code,{children:"wp_get_*"}),", ",(0,r.jsx)(e.code,{children:"is_*"}),", ",(0,r.jsx)(e.code,{children:"has_*"}),")"]})]}),"\n",(0,r.jsx)(e.p,{children:(0,r.jsx)(e.strong,{children:"Always provide fallbacks for:"})}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsx)(e.li,{children:"Plugin functions that might not exist"}),"\n",(0,r.jsx)(e.li,{children:"Functions that depend on external data"}),"\n",(0,r.jsx)(e.li,{children:"Custom functions from themes or plugins"}),"\n"]}),"\n",(0,r.jsxs)(e.p,{children:["Use ",(0,r.jsx)(e.code,{children:"php_function_output()"})," to bridge the gap between GraphQL queries and the full WordPress PHP ecosystem, enabling access to any WordPress, plugin, or custom functionality within your templates."]})]})}function h(n={}){const{wrapper:e}={...(0,o.R)(),...n.components};return e?(0,r.jsx)(e,{...n,children:(0,r.jsx)(d,{...n})}):d(n)}},7227:(n,e,t)=>{t.d(e,{A:()=>a});t(6540);var s=t(8215);const r={tabItem:"tabItem_Ymn6"};var o=t(4848);function a({children:n,hidden:e,className:t}){return(0,o.jsx)("div",{role:"tabpanel",className:(0,s.A)(r.tabItem,t),hidden:e,children:n})}},8453:(n,e,t)=>{t.d(e,{R:()=>a,x:()=>u});var s=t(6540);const r={},o=s.createContext(r);function a(n){const e=s.useContext(o);return s.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function u(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(r):n.components||r:a(n.components),s.createElement(o.Provider,{value:e},n.children)}},9489:(n,e,t)=>{t.d(e,{A:()=>w});var s=t(6540),r=t(8215),o=t(8630),a=t(4245),u=t(6347),i=t(6494),l=t(2814),c=t(5167),p=t(9900);function d(n){return s.Children.toArray(n).filter(n=>"\n"!==n).map(n=>{if(!n||(0,s.isValidElement)(n)&&function(n){const{props:e}=n;return!!e&&"object"==typeof e&&"value"in e}(n))return n;throw new Error(`Docusaurus error: Bad <Tabs> child <${"string"==typeof n.type?n.type:n.type.name}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.`)})?.filter(Boolean)??[]}function h(n){const{values:e,children:t}=n;return(0,s.useMemo)(()=>{const n=e??function(n){return d(n).map(({props:{value:n,label:e,attributes:t,default:s}})=>({value:n,label:e,attributes:t,default:s}))}(t);return function(n){const e=(0,c.XI)(n,(n,e)=>n.value===e.value);if(e.length>0)throw new Error(`Docusaurus error: Duplicate values "${e.map(n=>n.value).join(", ")}" found in <Tabs>. Every value needs to be unique.`)}(n),n},[e,t])}function f({value:n,tabValues:e}){return e.some(e=>e.value===n)}function m({queryString:n=!1,groupId:e}){const t=(0,u.W6)(),r=function({queryString:n=!1,groupId:e}){if("string"==typeof n)return n;if(!1===n)return null;if(!0===n&&!e)throw new Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return e??null}({queryString:n,groupId:e});return[(0,l.aZ)(r),(0,s.useCallback)(n=>{if(!r)return;const e=new URLSearchParams(t.location.search);e.set(r,n),t.replace({...t.location,search:e.toString()})},[r,t])]}function _(n){const{defaultValue:e,queryString:t=!1,groupId:r}
1=n,o=h(n),[a,u]=(0,s.useState)(()=>function({defaultValue:n,tabValues:e}){if(0===e.length)throw new Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(n){if(!f({value:n,tabValues:e}))throw new Error(`Docusaurus error: The <Tabs> has a defaultValue "${n}" but none of its children has the corresponding value. Available values are: ${e.map(n=>n.value).join(", ")}. If you intend to show no default tab, use defaultValue={null} instead.`);return n}const t=e.find(n=>n.default)??e[0];if(!t)throw new Error("Unexpected error: 0 tabValues");return t.value}({defaultValue:e,tabValues:o})),[l,c]=m({queryString:t,groupId:r}),[d,_]=function({groupId:n}){const e=function(n){return n?`docusaurus.tab.${n}`:null}(n),[t,r]=(0,p.Dv)(e);return[t,(0,s.useCallback)(n=>{e&&r.set(n)},[e,r])]}({groupId:r}),g=(()=>{const n=l??d;return f({value:n,tabValues:o})?n:null})();(0,i.A)(()=>{g&&u(g)},[g]);return{selectedValue:a,selectValue:(0,s.useCallback)(n=>{if(!f({value:n,tabValues:o}))throw new Error(`Can't select invalid tab value=${n}`);u(n),c(n),_(n)},[c,_,o]),tabValues:o}}var g=t(1062);const x={tabList:"tabList__CuJ",tabItem:"tabItem_LNqP"};var j=t(4848);function b({className:n,block:e,selectedValue:t,selectValue:s,tabValues:o}){const u=[],{blockElementScrollPositionUntilNextRender:i}=(0,a.a_)(),l=n=>{const e=n.currentTarget,r=u.indexOf(e),a=o[r].value;a!==t&&(i(e),s(a))},c=n=>{let e=null;switch(n.key){case"Enter":l(n);break;case"ArrowRight":{const t=u.indexOf(n.currentTarget)+1;e=u[t]??u[0];break}case"ArrowLeft":{const t=u.indexOf(n.currentTarget)-1;e=u[t]??u[u.length-1];break}}e?.focus()};return(0,j.jsx)("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,r.A)("tabs",{"tabs--block":e},n),children:o.map(({value:n,label:e,attributes:s})=>(0,j.jsx)("li",{role:"tab",tabIndex:t===n?0:-1,"aria-selected":t===n,ref:n=>{u.push(n)},onKeyDown:c,onClick:l,...s,className:(0,r.A)("tabs__item",x.tabItem,s?.className,{"tabs__item--active":t===n}),children:e??n},n))})}function v({lazy:n,children:e,selectedValue:t}){const o=(Array.isArray(e)?e:[e]).filter(Boolean);if(n){const n=o.find(n=>n.props.value===t);return n?(0,s.cloneElement)(n,{className:(0,r.A)("margin-top--md",n.props.className)}):null}return(0,j.jsx)("div",{className:"margin-top--md",children:o.map((n,e)=>(0,s.cloneElement)(n,{key:e,hidden:n.props.value!==t}))})}function y(n){const e=_(n);return(0,j.jsxs)("div",{className:(0,r.A)(o.G.tabs.container,"tabs-container",x.tabList),children:[(0,j.jsx)(b,{...e,...n}),(0,j.jsx)(v,{...e,...n})]})}function w(n){const e=(0,g.A)();return(0,j.jsx)(y,{...n,children:d(n.children)},String(e))}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.