{"id":5583,"date":"2017-06-12T16:35:57","date_gmt":"2017-06-12T14:35:57","guid":{"rendered":"http:\/\/wppa.nl\/?page_id=5583"},"modified":"2021-09-01T13:29:17","modified_gmt":"2021-09-01T11:29:17","slug":"custom-code","status":"publish","type":"page","link":"https:\/\/wppa.nl\/nl\/docs-by-subject\/advanced-topics\/custom-code\/","title":{"rendered":"Custom code"},"content":{"rendered":"<p>The maintenance procedures in <strong>Advanced settings -> Maintenance -> II: Clearing and other irreversable maintenance procedures<\/strong> of the <strong>Photo ALbums -> Settings<\/strong> admin page contain two places where user code (php) can be placed to execute on all albums or photos. The best practice is to edit the code first with a decent editor and copy\/paste it in the edit area in Item 35: Custom album proc or Item 36: Custom photo proc.<br \/>\nAfter placing the code in the edit area, click somewhere outside the edit box, and wait until the green checkmark <img decoding=\"async\" src=\"https:\/\/wppa.nl\/wp-content\/plugins\/wp-photo-album-plus\/img\/tick.png\" style=\"height:16px;\" \/> appears.<\/p>\n<p>The code is not checked on syntax errors or the existence of called functions. If there are fatal errors in the code, you will see an alertbox that reports an internal server error (500) as soon as you press the <input type=\"button\" style=\"background-color:#ccc;color:#000;padding:3px 0;border-radius:3px;font-weight:normal;text-transform:none;\" value=\"Start!\" \/> button.<\/p>\n<p>The box in <strong>Item 35<\/strong> is for code that acts on albums, the box in <strong>Item 36<\/strong> is for code that acts on photos.<\/p>\n<h6>Example 1<\/h6>\n<p>This code finds the values of <strong>IPTC<\/strong> tags 2#025 and adds this as tags to the photos:<\/p>\n<pre>&lt;?php\r\n\r\n\/\/ Find existing tags\r\n$old = $photo['tags'];\r\n\r\n\/\/ Find new tags to be added\r\n$add = $wpdb->get_col( \"SELECT `description` FROM `\" . WPPA_IPTC . \"` WHERE `photo` = \" . $photo['id'] . \" AND `tag` = '2#025'\" );\r\n\r\n\/\/ If there is something to add...\r\nif ( ! empty( $add ) ) {\r\n\r\n\t\/\/ Compose the new tags in the right - sanitized - format\r\n\t$new = wppa_sanitize_tags( $old . ',' . implode( ',', $add ) );\r\n\r\n\t\/\/ If something changed...\r\n\tif ( $old != $new ) {\r\n\r\n\t\t\/\/ Update the photo data\r\n\t\twppa_update_photo( array( 'id' => $photo['id'], 'tags' => $new ) );\r\n\r\n\t\t\/\/ Notyfy the indexing mechanism it has to update the index for the current photo\r\n\t\twppa_index_update( 'photo', $photo['id'] );\r\n\r\n\t\t\/\/ The list of existing tags must be remade when it is asked for\r\n\t\twppa_clear_taglist();\r\n\r\n\t\t\/\/ Tell what we did in the logfile\r\n\t\twppa_log( 'obs', 'Tag ' . implode( ',', $add ) . ' added to photo ' . $photo['id'] );\r\n\t}\r\n}\r\n<\/pre>\n<h6>Explanation of the code<\/h6>\n<ul>\n<li>The code starts with <code>&lt;?php<\/code> but does NOT end with <code>?&gt;<\/code><\/li>\n<li>The array <code>$photo<\/code> of the current photo is available. It contains all the fields as defined in wppa-setup.php:<br \/>\nid, album, ext, name, description, p_order, mean_rating, linkurl, linktitle, linktarget, owner, timestamp, status, rating_count, tags, alt, filename, modified, location, views, clicks, page_id, exifdtm, videox, videoy, thumbx, thumby, photox, photoy, scheduledtm, scheduledel, custom, stereo, crypt, magickstack, indexdtm. Most of these items are changeable by the function <code>wppa_update_photo()<\/code>, but not all. If you try to update a field that can not be updated, you will see an errormessage in the logfile.<\/li>\n<li>Use <code>wppa_update_photo();<\/code> with an array as argument. The array MUST contain the id of the photo: <code>'id' => $photo['id']<\/code> and one or more items to update.<\/li>\n<li>Always, when updating any item that may be used for searching, include a call to <code>wppa_index_update( 'photo', $photo['id'] );<\/code> to indicate the indexing system that it must review the indexes where <code>$photo['id']<\/code> is involved.<\/li>\n<li>When changing tags, call <code>wppa_clear_taglist();<\/code> to indicate that at some time a new list of all existing tags must be compiled.<\/li>\n<p>If you want to keep track of what is going on, include a call to <code>wppa_log( $arg1, $arg2 );<\/code> <code>$arg1<\/code> is an indicator of the type of the log, use 'Obs' for 'Observation'. <code>$arg2<\/code> should hold the text you want to log. Do not include HTML-tags, they will be stripped out; To indicate bold or italic, use the pseudo tag-pairs {b}{\/b} and {i}{\/i}<\/li>\n<\/ul>\n<h6>Example 2<\/h6>\n<p>This code fills the first two custom data fields with the size in inches or centimetres, as calculated from the values of the corresponding exif tags 0xA002, 0xA003, 0x011A, 0x011B and 0x0128.<\/p>\n<pre>\r\n&lt;?php\r\n\/\/ Find the image width and height\r\n$exif_width  = $wpdb->get_var( \"SELECT `description` FROM `\" . WPPA_EXIF . \"` WHERE `tag` = 'E#A002' AND `photo` = \" . $id );\r\n$exif_height = $wpdb->get_var( \"SELECT `description` FROM `\" . WPPA_EXIF . \"` WHERE `tag` = 'E#A003' AND `photo` = \" . $id );\r\n\r\n\/\/ Find the image x resolution\r\n$x_res = $wpdb->get_var( \"SELECT `description` FROM `\" . WPPA_EXIF . \"` WHERE `tag` = 'E#011A' AND `photo` = \" . $id );\r\n$x_res = explode( '\/', $x_res );\r\nif ( isset( $x_res[1] ) && $x_res[1] > 0 ) {\r\n    $x_res = $x_res[0] \/ $x_res[1];\r\n}\r\nelse {\r\n    $x_res = $x_res[0];\r\n}\r\n\r\n\/\/ Compute the width in res units\r\n$x_result = $x_res ? sprintf( \"%1.2f\", $exif_width \/ $x_res ) : '';\r\n\r\n\/\/ Find the image y resolution\r\n$y_res = $wpdb->get_var( \"SELECT `description` FROM `\" . WPPA_EXIF . \"` WHERE `tag` = 'E#011B' AND `photo` = \" . $id );\r\n$y_res = explode( '\/', $y_res );\r\nif ( isset( $y_res[1] ) && $y_res[1] > 0 ) {\r\n    $y_res = $y_res[0] \/ $y_res[1];\r\n}\r\nelse {\r\n    $y_res = $y_res[0];\r\n}\r\n\r\n\/\/ Compute the height in res units\r\n$y_result = $y_res ? sprintf( \"%1.2f\", $exif_height \/ $y_res ) : '';\r\n\r\n\/\/ Find the resolution unit ( inch\/cm\/undefined )\r\n$res_unit = $wpdb->get_var( \"SELECT `description` FROM `\" . WPPA_EXIF . \"` WHERE `tag` = 'E#0128' AND `photo` = \" . $id );\r\n\r\nswitch ( $res_unit ) {\r\n    case '2':\r\n        $x_result .= ' in.';\r\n        $y_result .= ' in.';\r\n        break;\r\n    case '3':\r\n        $x_result .= ' cm.';\r\n        $y_result .= ' cm.';\r\n        break;\r\n}\r\n\r\n\/\/ Get existing custom data fields\r\n$cust_fields = unserialize( wppa_get_photo_item( $id, 'custom' ) );\r\nfor ( $i=0; $i<10; $i++ ) {\r\n    if ( ! isset( $cust_fields[$i] ) ) {\r\n        $cust_fields[$i] = '';\r\n    }\r\n}\r\n\r\n\/\/ Update\r\n$cust_fields[0] = wppa_sanitize_custom_field( $x_result );\r\n$cust_fields[1] = wppa_sanitize_custom_field( $y_result );\r\nwppa_update_photo( array( 'id' => $id, 'custom' => serialize( $cust_fields ) ) );\r\n<\/pre>\n<p>Examples of the final result in Custom Data Field <em>w#cd0<\/em> and <em>w#cd1<\/em>: <code>12.50 cm.<\/code> <code>7.33 in.<\/code> etc.<br \/>\nMore about <em>Custom Data<\/em> is on <a href=\"http:\/\/wppa.nl\/docs-by-subject\/custom-data\/\" target=\"_blank\" rel=\"noopener\">this documantation page<\/a>.<br \/>\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The maintenance procedures in Advanced settings -> Maintenance -> II: Clearing and other irreversable maintenance procedures of the Photo ALbums -> Settings admin page contain two places where user code (php) can be placed to execute on all albums or photos. The best practice is to edit the code first with a decent editor and <a href='https:\/\/wppa.nl\/nl\/docs-by-subject\/advanced-topics\/custom-code\/' class='excerpt-more'>[...]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":5733,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5583","page","type-page","status-publish","hentry","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/pages\/5583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/comments?post=5583"}],"version-history":[{"count":3,"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/pages\/5583\/revisions"}],"predecessor-version":[{"id":6704,"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/pages\/5583\/revisions\/6704"}],"up":[{"embeddable":true,"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/pages\/5733"}],"wp:attachment":[{"href":"https:\/\/wppa.nl\/nl\/wp-json\/wp\/v2\/media?parent=5583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}