{"id":1851,"date":"2024-08-03T08:45:38","date_gmt":"2024-08-03T06:45:38","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=1851"},"modified":"2024-08-03T08:45:39","modified_gmt":"2024-08-03T06:45:39","slug":"13-buenas-practicas-de-codigo-php","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/cmss-frameworks\/wordpress-2\/wordpress-avanzado\/13-buenas-practicas-de-codigo-php\/","title":{"rendered":"13. Buenas pr\u00e1cticas de c\u00f3digo PHP"},"content":{"rendered":"\n<p>En este cap\u00edtulo vamos a optimizar el c\u00f3digo, ya que tenemos mezclado c\u00f3digo <strong>PHP<\/strong> con c\u00f3digo <strong>HTML.<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Hay una p\u00e1gina donde podemos ver las buenas pr\u00e1cticas que se deben utilizar:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/make.wordpress.org\/core\/handbook\/testing\/automated-testing\/phpunit\/\">https:\/\/make.wordpress.org\/core\/handbook\/testing\/automated-testing\/phpunit\/<\/a><\/li>\n<\/ul>\n\n\n\n<p>Es mejor utilizar <strong>comillas dobles<\/strong>. Vamos a trabajar nuestro archivo <em>content.php<\/em> a manera de plantilla con la funcion <strong><em>printf<\/em><\/strong> de <strong>php,<\/strong> la cual permite imprimir con comodines. Si somos desarrolladores <strong>PHP<\/strong> nos debemos olvidar de los echos, mejor utilizar <strong><em>printf.<\/em><\/strong> Esta es la sintaxis que vamos a utilizar en el <em>content.php<\/em><\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nprintf( '&lt;main class=\"item &nbsp;i-b &nbsp;w-70 &nbsp;main\"&gt;');\n\/\/ query_posts( 'order=ASC&amp;category_name=gigantes&amp;posts_per_page=3');\n\nif(have_posts()):\n&nbsp; &nbsp; while(have_posts()):\n&nbsp; &nbsp; &nbsp; &nbsp; the_post();\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ echo '&lt;p&gt;Imprimiendo entrada&lt;\/p&gt;';\n&nbsp; &nbsp; &nbsp; &nbsp; $template_html = '\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;article class=\"pos\"&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;h1&gt;%s&lt;\/h1&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;a href=\"%s\"&gt;%s&lt;\/a&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;%s - %s&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;%s&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;%s&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;%s&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p&gt;%s&lt;\/p&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div class=\"the-content\"&gt;%s&lt;\/div&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/article&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;hr&gt;\n&nbsp; &nbsp; &nbsp; &nbsp; ';\n\n&nbsp; &nbsp; &nbsp; &nbsp; printf($template_html,\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_title(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_permalink(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_date(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_time(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_excerpt(),\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ the_category(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_category_list(' - '),\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ the_tags(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_tag_list('&lt;i&gt;',' ', '&lt;\/i&gt;' ),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_author(),\n&nbsp; &nbsp; &nbsp; &nbsp; get_the_content()\n&nbsp; &nbsp; &nbsp; &nbsp; );\n&nbsp; &nbsp; endwhile;\nelse:\n&nbsp; &nbsp; printf ('&lt;p class=\"error\"&gt;No hay entradas&lt;\/p&gt;');\nendif;\nrewind_posts();\nprintf ( '&lt;\/main&gt;' );<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo vamos a optimizar el c\u00f3digo, ya que tenemos mezclado c\u00f3digo PHP con c\u00f3digo HTML.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1809,"menu_order":12,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-1851","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo vamos a optimizar el c\u00f3digo, ya que tenemos mezclado c\u00f3digo PHP con c\u00f3digo HTML.","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=1851"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1851\/revisions"}],"predecessor-version":[{"id":1853,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1851\/revisions\/1853"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/1809"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=1851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}