{"id":1890,"date":"2014-08-07T19:41:29","date_gmt":"2014-08-07T19:41:29","guid":{"rendered":"http:\/\/musilda.cz\/?p=1890"},"modified":"2014-08-07T19:41:29","modified_gmt":"2014-08-07T19:41:29","slug":"povinne-vlozeni-nahledoveho-obrazku-u-prispevku","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/povinne-vlozeni-nahledoveho-obrazku-u-prispevku\/","title":{"rendered":"Povinn\u00e9 vlo\u017een\u00ed n\u00e1hledov\u00e9ho obr\u00e1zku u p\u0159\u00edsp\u011bvku"},"content":{"rendered":"

V minul\u00e9m \u010dl\u00e1nku jsem se v\u011bnoval tomu, jak vylep\u0161it vzhled administrace, pokud na webu pracuje v\u00edce lid\u00ed. Dnes budu pokra\u010dovat v podobn\u00e9m duchu. Uk\u00e1\u017eeme si, jak p\u0159inutit u\u017eivatele, aby ke \u010dl\u00e1nku v\u017edy p\u0159ilo\u017eil n\u00e1hledov\u00fd obr\u00e1zek.<\/p>\n

Jak by n\u00e1m jist\u011b potvrdila \u0159ada provozovatel\u016f magaz\u00edn\u016f, lid\u00e9 jsou chybuj\u00edc\u00ed a \u010dasto je jednotn\u00fd vzhled je tak „rozbit\u00fd“ chyb\u011bj\u00edc\u00edm obr\u00e1zkem.<\/p>\n

V n\u00e1vodu vyu\u017eijeme dvou akc\u00ed – save_post<\/strong> a\u00a0admin_notices<\/strong>. Prvn\u00ed se vol\u00e1, kdy\u017e se \u010dl\u00e1nek ukl\u00e1d\u00e1 a druh\u00e1 se vol\u00e1, p\u0159i kontrole chybov\u00fdch hl\u00e1\u0161en\u00ed.<\/p>\n

Nejprve p\u0159id\u00e1me do hooku funkci:<\/p>\n

add_action('save_post', 'musilda_control_thumbnail');<\/pre>\n

Ve funkci samotn\u00e9 nejprve zkontrolujeme, zda jde o post, tedy \u010dl\u00e1nek. Obdobn\u011b m\u016f\u017eeme funkci roz\u0161\u00ed\u0159it, nebo upravit pro jin\u00e9 druhy obsahu. Pot\u00e9 zkontrolujeme, zda m\u00e1 \u010dl\u00e1nek n\u00e1hledov\u00fd obr\u00e1zek. Pokud ano, pro jistotu odstran\u00edme z\u00e1pis
\nchyby, pokud ne, vytvo\u0159\u00edme chybov\u00e9 hl\u00e1\u0161en\u00ed.<\/p>\n

N\u00e1sledn\u011b odstran\u00edme funkci z hooku, proto\u017ee jinak se n\u00e1m funkce zacykl\u00ed a bude se neust\u00e1le volat. Ulo\u017e\u00edme \u010dl\u00e1nek jako draft.<\/p>\n

\nfunction musilda_control_thumbnail($post_id){\n  if(get_post_type($post_id) != 'post')\n  return;\n\n  if ( !has_post_thumbnail( $post_id ) ) {\n    set_transient( \"has_post_thumbnail\", \"no\" );\n    remove_action('save_post', 'musilda_control_thumbnail');\n    wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));\n  }else{\n    delete_transient( \"has_post_thumbnail\" );\n  }\n}\n<\/pre>\n

Nyn\u00ed mus\u00edme p\u0159ipravit chybovou hl\u00e1\u0161ku. P\u0159iprav\u00edme si funkci do hooku:<\/p>\n

add_action('admin_notices', 'musilda_error');<\/pre>\n

Ve funkci pak zkontrolujeme existenci hodnoty a vytvo\u0159\u00edme html k\u00f3d s chybou:<\/p>\n

\nfunction musilda_error(){\n  if ( get_transient( \"has_post_thumbnail\" ) == \"no\" ) {\n  echo \"
\n

\n P\u0159\u00edsp\u011bvek mus\u00ed m\u00edt n\u00e1hledov\u00fd obr\u00e1zek!<\/strong>\n <\/p>\n <\/div>\";\ndelete_transient( \"has_post_thumbnail\" );\n }\n}\n<\/pre>\n

P\u0159i pokusu o publikaci, se \u010dl\u00e1nek nepublikuje a zobraz\u00ed se hl\u00e1\u0161ka:
\n\"Upravit<\/a><\/p>\n

Samoz\u0159ejm\u011b, \u017ee nejste omezeni jen na n\u00e1hledov\u00fd obr\u00e1zek, m\u016f\u017eete kontrolovat nap\u0159\u00edklad existenci custom field\u016f.<\/p>\n","protected":false},"excerpt":{"rendered":"

V minul\u00e9m \u010dl\u00e1nku jsem se v\u011bnoval tomu, jak vylep\u0161it vzhled administrace, pokud na webu pracuje v\u00edce lid\u00ed. Dnes budu pokra\u010dovat v podobn\u00e9m duchu. Uk\u00e1\u017eeme si, jak p\u0159inutit u\u017eivatele, aby ke \u010dl\u00e1nku v\u017edy p\u0159ilo\u017eil n\u00e1hledov\u00fd obr\u00e1zek. Jak by n\u00e1m jist\u011b potvrdila \u0159ada provozovatel\u016f magaz\u00edn\u016f, lid\u00e9 jsou chybuj\u00edc\u00ed a \u010dasto je jednotn\u00fd vzhled je tak „rozbit\u00fd“ chyb\u011bj\u00edc\u00edm<\/p>\n","protected":false},"author":1,"featured_media":1484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[12,19,30],"tags":[],"class_list":["post-1890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-navody","category-snippety","category-wordpress"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1890"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=1890"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/1890\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/1484"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=1890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=1890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=1890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}