Nyílt forráskódú (PHP és MySQL alapú) tartalomkezelő rendszer.
A világ top 1 millió weboldalából 16,7% WordPress alapú (forrás Alexa) és az új weboldalak 22%-a készül WordPress-el. Ha a megrendelő másképp nem rendelkezik, akkor mi is ennek az open source rendszernek a segítségével építjük fel a honlapjainkat. Ennek előnyeiről és költséghatékonyságáról egy korábbi cikkemben ide kattintva olvashatsz.
Ez a cikk viszont azoknak szól, akik aktívan használják azt és van némi tapasztalatuk a programozásban. Mi mindig saját, egyedi webdesignt és hozzá tartozó sablont készítünk. Az alábbi kódrészleteket olykor mi is használjuk egy-egy témában.
Ez a kódsorozat minden felesleges csatolt wordpress file-t elrejt a forráskód head részében. Célszerű beilleszteni a functions.php-ba, hogy jobban átlássuk az oldal forráskódját.
remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0); remove_action(‘wp_head’, ‘start_post_rel_link’, 10, 0); remove_action(‘wp_head’, ‘wp_generator’); remove_action(‘wp_head’, ‘rsd_link’); remove_action(‘wp_head’, ‘wlwmanifest_link’); remove_action(‘wp_head’, ‘feed_links_extra’,3); remove_action(‘wp_head’, ‘rel_canoncial’); remove_action(‘wp_head’, ‘wp_shortlink_wp_head’); remove_action(‘wp_head’, ‘feed_links’, 2); remove_action(‘wp_head’, ‘index_rel_link’); remove_action(‘wp_head’, ‘rel_next’); remove_action(‘wp_head’, ‘parent_post_rel_link’, 10, 0); remove_action(‘wp_head’, ‘locale_stylesheet’); remove_action(‘wp_head’, ‘noindex’); remove_action(‘wp_head’, ‘wp_print_styles’); remove_action(‘wp_head’, ‘wp_print_head_scripts’); remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7 ); remove_action( ‘admin_print_scripts’, ‘print_emoji_detection_script’ ); remove_action( ‘wp_print_styles’, ‘print_emoji_styles’ ); remove_action( ‘admin_print_styles’, ‘print_emoji_styles’ ); remove_action(‘wp_head’, ‘wp_generator’);
Bekapcsolja a keresési lehetőséget a wordpress bejegyzések egyedi mezői között is. A $custom_fields nevű változóban tömbökben egyesével kell megadni a keresni kívánt mezőket.
function custom_search_where($pieces) {
(is_search() && !is_admin()) {
global $wpdb; $custom_fields = array(‘cim’,’cegvezeto’,’telefon’,’mobil’,’szlogen’,’email’,’webcim’); $keywords = explode(‘ ‘, get_query_var(‘s’)); $query = “”; foreach ($custom_fields as $field) { foreach ($keywords as $word) { $query .= “((mypm1.meta_key = ‘”.$field.”‘)”; $query .= ” AND (mypm1.meta_value LIKE ‘%{$word}%’)) OR “; } } if (!empty($query)) { $pieces[‘where’] = str_replace(“((({$wpdb->posts}.post_title LIKE ‘%”, “( {$query} (({$wpdb->posts}.post_title LIKE ‘%”, $pieces[‘where’]); $pieces[‘join’] = $pieces[‘join’] . ” INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)”; $pieces[‘groupby’] = “{$wpdb->posts}.ID”; }} return ($pieces); } add_filter(‘posts_clauses’, ‘custom_search_where’, 20, 1);
A wp TinyMCE szerkesztője eredetileg nem jól kezeli a sortöréseket és van, hogy bennhagyja vagy épp ellenkezőleg kitörli a beállított paragrafusokat. Az alábbi kód kijavítja a hibát.
function wp_split_more($content, $type, $more_html = null) {function wp_split_more($content, $type, $more_html = null) { $return = [ “before” => null, “after” => null ]; if ( preg_match(‘/<!–more(.*?)?–>/’, $content, $matches) ) { list($return[“before”], $return[“after”]) = explode($matches[0], $content, 2); } else { $return[“before”] = $content; $return[“after”] = ”; } foreach($return as $key=>$value) { // remove whitespace $return[$key] = trim(preg_replace(‘/^[\s]*(.*)[\s]*$/’, ‘\\1’, $return[$key])); // remove falsly opening p tag if( strpos($return[$key], “</p>”) === 0 ) { $return[$key] = trim(substr($return[$key], strlen(“</p>”))); } // remove opening p tag if( strpos($return[$key], “<p>”) === 0 ) { $return[$key] = trim(substr($return[$key], strlen(“<p>”))); } // remove closing p tag if( strrpos($return[$key], “<p>”) === strlen($return[$key])-strlen(“<p>”) ) { $return[$key] = trim(substr($return[$key], 0, strlen($return[$key])-strlen(“<p>”))); } if( strrpos($return[$key], “</p>”) === strlen($return[$key])-strlen(“</p>”) ) { $return[$key] = trim(substr($return[$key], 0, strlen($return[$key])-strlen(“</p>”))); } } // add more tag if( $more_html !== null && $return[“after”] !== “” ) { $return[“before”] = $return[“before”].””.$more_html; } // add opening / closing p-tags $return[“before”] = “<p>”.$return[“before”].”</p>”; $return[“after”] = “<p>”.$return[“after”].”</p>”; return $return[$type]; }
Sidebar támogatás az egyedi témánkhoz.
add_action( ‘widgets_init’, ‘my_register_sidebars’ );add_action( ‘widgets_init’, ‘my_register_sidebars’ ); function my_register_sidebars() { register_sidebar( array( ‘name’ => ‘primary’, ‘description’ => ‘Jobb oldali sidebar’, ‘id’ => ‘primary’, ‘before_widget’ => ‘<div class=”next_to”>’, ‘after_widget’ => ‘</div>’, ‘before_title’ => ‘<br /><h2 class=”next_to_title”>’, ‘after_title’ => ‘</h2><hr />’ ) ); }
Az exceprt hozzáadása a wp függvényekhez. Plusz egy használható mezőnk lesz így minden oldalon…
function wpcodex_add_excerpt_support_for_pages() {function wpcodex_add_excerpt_support_for_pages() { add_post_type_support( ‘page’, ‘excerpt’ ); } add_action( ‘init’, ‘wpcodex_add_excerpt_support_for_pages’ );
Vélemény, hozzászólás?